转载出处:http://www.ezineasp.net/post/ASP-Net-Bind-GridView-to-DataTable.aspx
ASP.Net Bind GridView to DataTable
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 = new DataTable();
// Initialize DataColumn
DataColumn myDataColumn = new DataColumn();
// 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 = new DataColumn();
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 = new DataColumn();
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:
![](https://i-blog.csdnimg.cn/blog_migrate/aa7afc90934ad504c68eb6fd86f47006.png)
转载于:https://blog.51cto.com/hduzx/868461