Friday, May 16, 2008

Data Row dynamically adding to DataTable

DataTable tbl = new DataTable();
tbl.Columns.Add("ColumnA");

tbl.Columns.Add("ColumnB");

tbl.Columns.Add("ColumnC");

for(int i = 0; i<10; i++)

{

DataRow nr = tbl.NewRow();

nr["ColumnA"] = "A" + i.ToString();

nr["ColumnB"] = "B" + i.ToString();

nr["ColumnC"] = "C" + i.ToString();

tbl.Rows.Add(nr);

}

PrintRows(tbl);
}


private static void PrintRows(DataTable tbl)
{

for(int i=0; i
{

Console.WriteLine("row: {0}, ColumnA: {1}, ColumnB: {2}", i, tbl.Rows[i]["ColumnA"], tbl.Rows[i]["ColumnB"]);

}

}