Wednesday, July 30, 2008

How to Create DataTable Programmatically in C#, ASP.NET ?

Most of the times programmers fill the DataTable from database. This action fills the schema of the database table into the DataTable. We can bind data controls like GridView, DropdownList to such DataTable. But somtimes what happens is one needs to create a DataTable programmatically. Here I have put the simple method of creating DataTable programmatically in C#.

Update: Responding to comment from one of the readers, I have now posted how to create DataTable in c# with clickable column. As in this post, the DataTable has been bound to an asp.net GridView.

Create a DataTable instance

DataTable table = new DataTable();

Create 7 columns for this DataTable
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Name");
DataColumn col3 = new DataColumn("Checked");
DataColumn col4 = new DataColumn("Description");
DataColumn col5 = new DataColumn("Price");
DataColumn col6 = new DataColumn("Brand");
DataColumn col7 = new DataColumn("Remarks");

Define DataType of the Columns
col1.DataType = System.Type.GetType("System.Int");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.Boolean");
col4.DataType = System.Type.GetType("System.String");
col5.DataType = System.Type.GetType("System.Double");
col6.DataType = System.Type.GetType("System.String");
col7.DataType = System.Type.GetType("System.String");

Add All These Columns into DataTable table
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
table.Columns.Add(col4);
table.Columns.Add(col5);
table.Columns.Add(col6);
table.Columns.Add(col7);

Create a Row in the DataTable table
DataRow row = table.NewRow();
Fill All Columns with Data
row[col1] = 1100;
row[col2] = "Computer Set";
row[col3] = true;
row[col4] = "New computer set";
row[col5] = 32000.00
row[col6] = "NEW BRAND-1100";
row[col7] = "Purchased on July 30,2008";

Add the Row into DataTable
table.Rows.Add(row);
Want to bind this DataTable to a GridView?
GridView gvTest=new GridView();
gvTest.DataSource = table;
gvTest.DataBind();

You are done! Happy dot netting!!
Shout it

19 comments:

Andy Mahood said...

superb explanation to something that I am always forgetting.

Lord Hagen said...

thnks. I have to use two datatable, one to get data from a MySql query and the second one to change the datatype in some columns. The second datatable was made with your explanation. Thnks again.

Anonymous said...

can you make the rows clickable?

Anonymous said...

thanks I used your explanation.
Adalberto Montania
from asuncion Paraguay

Anonymous said...

Its nice! but if u explained for adding multiple rows in a data table ...it will be very useful to us...

十二月 said...

Hi, i'm a newbie here. Is that possible if i want to store the data from the excel file into different datatable??

For example, there is 1 column that consists of 5 datarow in the excel file, i want to store them into different datatable.
First data in datatable1, second data in datatable2, etc. So that later i can merge them into a gridview.

Is that possible?

Regards,
Howyi

shikha said...

really nice help how can i catch value of row in label control from data table...............plz guide me

micle said...

Great post

We can also submit our .net related article links on http://www.dotnettechy.com to increase traffic

Anonymous said...

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

will return a null.

Try using Int16, Int32 or Int64.

Anonymous said...

Nice post... its very helpfull... lol...

Mayank Topiwala said...

Now how to close this DataTable in C# as we can do in VB (table.close)

Anonymous said...

Superb Postt It help a lot. Awesome dude.

Anonymous said...

Awsome.. I will never forget this technique.
Bhaskar

Anonymous said...

that was great
thanx

Anonymous said...

very helpful explaination. but I want to add multiple rows in datatable and display it in gridview. How can I do it?

Anonymous said...

it is very useful code for me

RaM said...

thanks i was searching exactly for this..

Anonymous said...

If I put the code in my *aspx.cs file in protected void Page_Load(object sender, EventArgs e), how do I get the table to show up - in other words, what do I do in the *aspx file? This doesnt work:



Anonymous said...

Thanks for the help! Very Useful!!!!!

Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles