Binding a GridView control programmatically with datasetBinding a GridView with a dataset is done after creating the dataset. To create a DataSet, we need to follow the steps given below.
- Declare the string variable 'connectionString' to hold the Connection String
- Declare the Connection object
- Declare the string variable 'str_Account_Select' to hold the SQL statement
- Declare the Command object
- Declare the DataSet object
- Instantiate the Connection object
- Open the connection
- Instantiate the Command object
- Instantiate the DataSet object
- Instantiate the SqlDataAdapter object
- Populate the DataSet
After creating the dataset, we can bind the GridView with a dataset. To bind the GridView control programmatically, we set DataSource property and call DataBind() method of the GridView control in the Page_Load() method.
In the following code example, the GridView control is bound to the DataSet object. After the DataSource property is set, the DataBind() method is called explicitly. We need to place the following code in a .aspx page to create a GridView control.
<asp:GridView ID="GridView1" runat="server" />In the Page_Load method, we place the following code to bind the GridView with a dataset.
Become an Expert
People also viewed: Create your own ERP Software using ASP Core>>