gpt4 book ai didi

c# - 如何动态地将列和行添加到空数据集?

转载 作者:行者123 更新时间:2023-11-30 15:08:20 25 4
gpt4 key购买 nike

我创建了一个新的数据集作为

DataSet local_ds2 = new DataSet();

我试过动态添加行和列

DataColumn dcAmount = new DataColumn("EmpID");
local_ds2.Tables["ACHFile"].Columns.Add(dcAmount);

DataColumn dcName = new DataColumn("Name");
local_ds2.Tables["ACHFile"].Columns.Add(dcName);

DataColumn dcBnkRoutingNumber = new DataColumn("BankRoutingNumber");
local_ds2.Tables["ACHFile"].Columns.Add(dcBnkRoutingNumber);

DataColumn dcBnkAccount = new DataColumn("BankAccount");
local_ds2.Tables["ACHFile"].Columns.Add(dcBnkAccount);

DataColumn dc = new DataColumn("Amount");
local_ds2.Tables["ACHFile"].Columns.Add(dc);

DataColumn dc1 = new DataColumn("BankAccountTypeID");
local_ds2.Tables["ACHFile"].Columns.Add(dc1);

for (int i = 0; i < chkcnt; i++)
{
local_ds2.Tables["ACHFile"].Rows[i]["EmpID"] = EmpID[i];
local_ds2.Tables["ACHFile"].Rows[i]["Name"] = Empname[i];
local_ds2.Tables["ACHFile"].Rows[i]["BankRoutingNumber"] = BnkRoutingNumber[i];
local_ds2.Tables["ACHFile"].Rows[i]["BankAccount"] = BnkAccount[i];
local_ds2.Tables["ACHFile"].Rows[i]["BankAccountTypeID"] = AchDB.strBankTypeID[i];
local_ds2.Tables["ACHFile"].Rows[i]["Amount"] = AchDB.Amount1[i];
if (AchDB.strBankTypeID[i].ToString() == "D")
strBankAccntType = "BankAccountTypeID='" + AchDB.strBankTypeID[i].ToString() + "'";
}

但是我无法动态添加列和行,谁能帮帮我

最佳答案

试着把你的代码改成这样

DataRow dr;        

for (int i = 0; i < chkcnt; i++)
{
dr = local_ds2.Tables["ACHFile"].NewRow();
dr["EmpID"] = EmpID[i];
dr["Name"] = Empname[i];
dr["BankRoutingNumber"] = BnkRoutingNumber[i];
dr["BankAccount"] = BnkAccount[i];
dr["BankAccountTypeID"] = AchDB.strBankTypeID[i];
dr["Amount"] = AchDB.Amount1[i];
if (AchDB.strBankTypeID[i].ToString() == "D")
strBankAccntType = "BankAccountTypeID='" + AchDB.strBankTypeID[i].ToString() + "'";
local_ds2.Tables["ACHFile"].Rows.Add(dr);
}

您没有将行添加到数据表

关于c# - 如何动态地将列和行添加到空数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5712509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com