From the namespace
System.Data
DataTable DataColumn classes we can easily create table dynamically in C# and also in ASP.NET.
using System.Data;
DataTable dTbl = new DataTable("myDynamicTable");
DataColumn dValue = new DataColumn();
DataColumn dMember = new DataColumn();
dValue.ColumnName = "Id";
dValue.DataType = Type.GetType("System.Int32");
dMember.ColumnName = "Name";
dMember.DataType = Type.GetType("System.String");
dTbl.Columns.Add(dValue);
dTbl.Columns.Add(dMember);
After you create table dynamically, you can add rows into it.
It is a good way to create DataRow from the table we create, with the function
NewRow()
.
DataRow myrow = dTbl.NewRow();
myrow["Id"] = 1;
myrow["Name"] = "Tux";
dTbl.Rows.Add(myrow);
That's all for
creating table dynamically
in C# and also ASP.NET.