From the namespace System.Data DataTable DataColumn classes we can easily create table dynamically in C# and also in ASP.NET.
using System.Data;After you create table dynamically, you can add rows into it.
// Create a DataTable instance
DataTable dTbl = new DataTable("myDynamicTable");
// Create a DataColumn instances
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");
// Add these DataColumns into the DataTable
dTbl.Columns.Add(dValue);
dTbl.Columns.Add(dMember);
It is a good way to create DataRow from the table we create, with the function NewRow().
// Create a DataRow Instance from the table we create above, with NewRow();That's all for creating table dynamically in C# and also ASP.NET.
DataRow myrow = dTbl.NewRow();
myrow["Id"] = 1;
myrow["Name"] = "Tux";
// Add the row into the table
dTbl.Rows.Add(myrow);
7 comments:
thank yuuu
Thanks. This code has really help me
thanks!! great example :)
Nice Example....
How to bind data from database in datatable asp.netC#
Not Displaying anything at run time
how to copy a table at runtime
Post a Comment