Binding a GridView control programmatically with dataset
Binding 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.
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string connectionString = "server=SYS2;" + "integrated security=SSPI;" + "database=ERPFinAccounting"; SqlConnection myConnection; string str_Account_Select = "SELECT * FROM AccountsTable"; SqlCommand myCommand; DataSet myDataSet; myConnection = new SqlConnection(connectionString); myConnection.Open(); myCommand = new SqlCommand(str_Account_Select, myConnection); SqlDataAdapter mySQLDataAdapter; myDataSet = new DataSet(); mySQLDataAdapter = new SqlDataAdapter(myCommand); mySQLDataAdapter.Fill(myDataSet, "AccountsTable"); GridView1.DataSource = myDataSet; GridView1.DataBind(); } }
GridView Articles
- GridView AutoGenerateColumns property
- Bind GridView control with SqlDataSource control
- DataBinding in a GridView control
- Binding a GridView programmatically with Generic List Collection
- Using fields with the GridView control
- Adding Boundfields to a GridView control
- Adding button fields to a GridView control
- Create Templatefields in a GridView control
- Access a value using SelectedIndexChanged() event when a row in a GridView is selected
- How to restrict an item from being selected using GridView SelectedIndexChanging Event
- How to add a new row in the GridView control by using the GridView footer template
- Gridview datakeynames
- How to select a particular row in a GridView control using SelectedDataKey method
- Formatting GridView control with CSS
- GridView rowDataBound event
Become an Expert
Learn More
Q1. What is Azure Platform?
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net