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(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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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);
.......
}
Method 3
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0