How do create a DataTable in C#?
I did like this:
DataTable dt = new DataTable();
dt.clear();
dt.Columns.Add(\"Name\");
dt.Columns.Add(\"Marks\")
Question 1: How do create a DataTable in C#?
Answer 1:
DataTable dt = new DataTable(); // DataTable created
// Add columns in your DataTable
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
Note: There is no need to Clear()
the DataTable
after creating it.
Question 2: How to add row(s)?
Answer 2: Add one row:
dt.Rows.Add("Ravi","500");
Add multiple rows: use ForEach
loop
DataTable dt2 = (DataTable)Session["CartData"]; // This DataTable contains multiple records
foreach (DataRow dr in dt2.Rows)
{
dt.Rows.Add(dr["Name"], dr["Marks"]);
}