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
myDataTable.Columns.Add(myDataColumn);


// 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
myDataTable.Columns.Add(myDataColumn);

// 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
myDataTable.Columns.Add(myDataColumn);

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.
myDataTable.Rows.Add(dataRow);

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

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

myDataTable.Rows.Add(dataRow);

Step 3:

C# Code to bind ASP.Net GridView to DataTable

GridView1.DataSource = myDataTable;
GridView1.DataBind();

HTML Source Code for GridView bound to a DataTable

<asp:GridView ID="GridView1" 
    runat="server" 
    AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField 
            DataField="auto_ID" 
            HeaderText="Auto ID" />
        <asp:BoundField 
            DataField="firstName" 
            HeaderText="First Name" />
        <asp:BoundField 
            DataField="lastName" 
            HeaderText="Last Name" />
    </Columns>
</asp:GridView>

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"
fasfasdf
it is very helpful
abhay
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
hikizume976
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.
bloodybenny
Another trivial example from the hordes of amateur programmers. Snooze.
Thanks for the nice explanation of the article.
Shiney
very good article. Thanks for your sharing...
Tomasz
I am also looking for solution how update/detele rows in DataTable using GridView.
Asif
yes, this is very useful article especially for beginners.
ana
good
Syam>S
Thanks a lot ... it really Helped me ..... Thank u very much ...Good Resource...
This site is really good one.
Prabhat
Thanks for the solution. It has solved my problem.
naresh
hello sir. its very helpful.
i have a question.
please give me the answer.

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

Please explain your problem more clearly, what are you trying to do?
purshottam
Thanks a lot. its very help full for me. Thank u very much. very good step by step explation shown.
VEnkataReddy
Thanks you so much,Its Very helpfull for Beginers also.. Having with good explanation comments.
good
Thank's for helpfull.
Ahmed Nasr
Very good example thank you
nithya
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)...
anuja
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 http://code.google.com/p/data-table-data-source/
Riyaz
Thanks for the example. Explanation is done in very simple and understandable way. Very useful for the ASP.net newcomers.
Nitin
this is very helpful for those who are begginer in asp.net.
irfan
good
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email