ASP.Net Bind GridView to DataTable

Updated on 31 Jul 2012,
Published on 03 Sep 2008

It's not a difficult task in ASP.Net to bind GridView to DataTable. You can bind a GridView to DataTable in 3 easy steps. Create a DataTable, add data rows to the DataTable and last bind the DataTable to GridView to display the data in Tabular form. In the previous tutorial about How to Add New ASP.Net DataRow to DataTable we learnt how to create a DataTable and add a new row to the table. In this tutorial we will consider the same example to understand the DataTable and bind the GridView to DataTable created dynamically using C# code.

Step 1:

C# Code to Create ASP.Net DataTable

// Initialize a DataTable
DataTable myDataTable = newDataTable();

// Initialize DataColumn
DataColumn myDataColumn = newDataColumn();

// Add First DataColumn
// AllowDBNull property
myDataColumn.AllowDBNull = false;

// set AutoIncrement property to true
myDataColumn.AutoIncrement = true;

// set AutoIncrementSeed property equal to 1
myDataColumn.AutoIncrementSeed = 1;

// set AutoIncrementStep property equal to 1
myDataColumn.AutoIncrementStep = 1;

// set ColumnName property to specify the column name
myDataColumn.ColumnName = "auto_ID";

// set DataType property of the column as Integer
myDataColumn.DataType = System.Type.GetType("System.Int32");

// set Unique property of DataColumn to true to allow unqiue value for this column in each row
myDataColumn.Unique = true;

// Add and Create a first DataColumn

// Add second DataColumn
// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = newDataColumn(); 
myDataColumn.ColumnName = "firstName";

// set DataType property of the column as String
myDataColumn.DataType = System.Type.GetType("System.String");

// Add and Create a Second DataColumn

// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = newDataColumn(); 
myDataColumn.ColumnName = "lastName";

// set DataType property of the column as String
myDataColumn.DataType = System.Type.GetType("System.String");

// Add and Create a Third DataColumn

Step 2:

C# Code to Add New Rows to ASP.Net DataTable

// create a new row using NewRow() function of DataTable.
// dataRow object will inherit the schema of myDataTable to create a new row
DataRow dataRow = myDataTable.NewRow(); 

dataRow["firstName"] = "John"; 
dataRow["lastName"] = "Smith";

// add new data row to the data table.

// similarly adds the second row to the DataTable
dataRow = myDataTable.NewRow();

dataRow["firstName"] = "Will"; 
dataRow["lastName"] = "Smith"; 


Step 3:

C# Code to bind ASP.Net GridView to DataTable

GridView1.DataSource = myDataTable;

HTML Source Code for GridView bound to a DataTable

<asp:GridView ID="GridView1" 
            HeaderText="Auto ID" />
            HeaderText="First Name" />
            HeaderText="Last Name" />

Above code will generate the following output:

Continue to next tutorial: ASP.Net Gridview DropDownList Column to learn how to work with gridivew templates to add a dropdownlist column.

25 Responses to "ASP.Net Bind GridView to DataTable"
it is very helpful
thanks so much, u sloved my problem. this is a great article for those who want to bind grid view dynamically, i was missing the last step of "HTML Source Code for GridView bound to a DataTable" and was wondering why even after using gridview1.databind i was not seein it in affect. thanks so much
too bad I didn't find this earlier as I managed to get the gridview working on my own. it would've been a lot easier. But now I'm stuck trying to update/delete rows. I bet that'd be useful to the same people who are looking forward to working with gridviews using this method.
Another trivial example from the hordes of amateur programmers. Snooze.
Thanks for the nice explanation of the article.
very good article. Thanks for your sharing...
I am also looking for solution how update/detele rows in DataTable using GridView.
yes, this is very useful article especially for beginners.
Thanks a lot ... it really Helped me ..... Thank u very much ...Good Resource...
This site is really good one.
Thanks for the solution. It has solved my problem.
hello sir. its very helpful.
i have a question.
please give me the answer.

how to bind the video data(video clips) to gridview?
can u tell how to bind the data from the database and binding to each column
Hi Pradeep

Please explain your problem more clearly, what are you trying to do?
Thanks a lot. its very help full for me. Thank u very much. very good step by step explation shown.
Thanks you so much,Its Very helpfull for Beginers also.. Having with good explanation comments.
Thank's for helpfull.
Ahmed Nasr
Very good example thank you
Excellent Work.... as per your code i displayed the data in gridview dynamically... but now, the pblm is have to store the gridview datas in database (sql 2005)...
it's very good example but i have one question? how to store the gridview data in database
That's all well and good, issue is that you can not allow for editing of the data with out jumping through a couple of hoops to get the data back into the data table. Try this
Thanks for the example. Explanation is done in very simple and understandable way. Very useful for the newcomers.
this is very helpful for those who are begginer in
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email