Pages

Wednesday, May 4, 2011

Create a DataTable Dynamically In C# .

Create a DataTable Dynamically In C# .

Introduction : In this article i will show you how to create DataTable Dynamically in c# .Many times we require to create a DataTable Dynamically in our code .It is very easy you just need to declare all the DataColumn and need to add in Datatable.

Dynamic DataTable code in c# :

//--Decalration Of Data Table ---//
DataTable Dt = new DataTable();

//--Decalration Of Data Column-----//
DataColumn DCID = new DataColumn("ID", typeof(Int32));
DataColumn DCName = new DataColumn("Name", typeof(String));
DataColumn DCCustomerID = new DataColumn("CustomerID", typeof(Int32));

//-- Add Data Column to DataTable --//
Dt.Columns.Add(DCID);
Dt.Columns.Add(DCName);
Dt.Columns.Add(DCCustomerID);


//-- Add Data Rows to DataTable --//
DataRow Dr1 = Dt.NewRow();
Dr1["ID"] = 1;
Dr1["Name"] = "Hamid Seta";
Dr1["CustomerID"] = 1;

DataRow Dr2 = Dt.NewRow();
Dr2["ID"] = 2;
Dr2["Name"] = "Ankur Shah";
Dr2["CustomerID"] = 2;

Dt.Rows.Add(Dr1);
Dt.Rows.Add(Dr2);



Related Other posts

2 comments: