Friday, April 13, 2012
You can create DataTable class object by using DataTable constructor, DataTable class object create an in-memnory table.
In this example, you will see how to create a DataTable in asp.net web application with two columns named “FullName” and “Age” and then I will add new row by using NewRow method. After I will create a new row then I will assign full name and age data to DataRow and I will add new DataRow object to DataTable.
Here’s an example
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("FullName", typeof(string)));
dt.Columns.Add(new DataColumn("Age", typeof(Int16)));
dr = dt.NewRow();
dr["FullName"] = "Aamir Hasan";
dr["Age"] = 20;
I have debug the code and display it in above screen shot as the datatable has been created successfully, data rows have been added and data has been entered.