Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Sunday, July 29, 2012

Create DataTable Programmatically in C#, ASP.NET

In this article we will discuss how to create a data table at runtime using C#.Net in Asp.Net. You can also check my last article on how to create thumbnail at the time of uploading file in Asp.Net fileupload control, Click here.

We can create Datatable using C#.Net. Below is the syntax:

DataTable table = new DataTable();

//Below we are defining the columns.

table.Columns.Add("ID", typeof(int));
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("DateOfBirth", typeof(DateTime));
Or also we can use below way to define the column:

DataColumn ID = new DataColumn("ID");
col1.DataType = System.Type.GetType("System.Int");

Here we can add data to the column like below:
table.Rows.Add(25, "SharePointDotnet", DateTime.Now);

Similarly below is an alternative way to add a row:

DataRow row = table.NewRow();
row[ID] = 1100;
row[FirstName] = "SharePointDotnet";
row[DateOfBirth] = "29-07-2012";

0 on: "Create DataTable Programmatically in C#, ASP.NET"