C# GridView
In C#, the GridView control is a powerful tool used to display, edit and delete data in a tabular format. It is used to display large amounts of data and offers many built-in features for sorting, paging, and filtering. In this tutorial, we'll discuss how to use GridView in C#.
Syntax
The syntax for using GridView control in C# is as follows:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField DataField="FieldName" HeaderText="Header Text" />
</Columns>
</asp:GridView>
The GridView control is typically embedded in an .aspx file with a set of columns, which are defined with BoundFields. BoundFields map to the fields of the data source and display them in the GridView.
Example
Let's say we have a table in our SQL Server database called "Employees". We can connect to this database and display the data in a GridView by following these steps:
- Create a connection to the SQL Server database.
- Define a query to select the data from the "Employees" table.
- Create a SqlDataAdapter object and fill a DataTable using the query.
- Bind the DataTable to the GridView control.
protected void BindGridView() {
// Step 1: Create a connection to the SQL Server database
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
// Step 2: Define a query to select the data from the "Employees" table
string query = "SELECT * FROM Employees";
// Step 3: Create a SqlDataAdapter object and fill a DataTable using the query
SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
// Step 4: Bind the DataTable to the GridView control
GridView1.DataSource = dt;
GridView1.DataBind();
}
Output
The output of the example code above is a gridview displaying the data from the "Employees" table in a tabular format.
Explanation
In the example above, we created a method called "BindGridView" that connects to a SQL Server database, selects all the data from the "Employees" table, and binds the data to the GridView control.
We first created a SqlConnection object and passed it the connection string from the web.config file. We then defined a query to select all the data from the "Employees" table and used it to create a SqlDataAdapter object. We then filled a DataTable object with data from the query using the SqlDataAdapter's Fill method.
Finally, we set the GridView's DataSource property to the DataTable and called the DataBind method to display the data in the GridView control.
Use
GridView is used when we want to display data in a tabular format. GridView allows us to easily display large amounts of data and provides built-in features for filtering and sorting data.
Important Points
- The GridView control requires a data source to display the data.
- The columns in the GridView are defined by BoundFields.
- GridView offers many built-in features for sorting, paging, and filtering data.
Summary
In this tutorial, we discussed how to use GridView control in C#. We covered the syntax, example, output, explanation, use, and important points of GridView in C#. With this knowledge, you can now use GridView to display data in a tabular format and utilize its built-in features for sorting, paging, and filtering data.