I created a new dataset as
DataSet local_ds2 = new DataSet();
I tried this to add rows and columns dynamically
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(开发者_C百科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() + "'";
}
But I am unable to add columns and rows dynamically can any one help me
Try changing your code to this
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);
}
You are not adding the rows to the Datatable
should be like...
DataRow myRow;
DataTable ACHFile = new DataTable();
DataColumn EmpID = new DataColumn("EmpID", System.Type.GetType("System.String"));
ACHFile.Columns.Add(EmpID );
for (int i = 0; i < chkcnt; i++)
{
myRow = ACHFile.NewRow();
myRow["EmpID"] = EmpID[i];
ACHFile.Rows.Add(myRow);
.......
}
try putting the datacolums in a tablerow and then adding the tablerow to the table. Right now it looks like you're adding a column to a table skipping the step of the rows
精彩评论