ASP.NET Web Forms

What is the purpose of the GridView control


The GridView control in ASP.NET Web Forms is a powerful data-bound control that displays data in a tabular format. It is widely used for presenting data from various data sources, such as databases, XML files, or collections. The GridView control provides built-in features for sorting, paging, editing, and deleting data, making it an essential tool for developers when creating data-driven web applications.

Key Features of the GridView Control

  • Data Binding: The GridView control can bind to various data sources, allowing for dynamic data display.
  • Sorting: Users can sort data by clicking on column headers.
  • Paging: The GridView can display a subset of data at a time, improving performance and usability.
  • Editing and Deleting: Built-in support for editing and deleting rows directly within the grid.
  • Template Support: Customizable templates for displaying data in various formats.

Sample Code for Using GridView

Below is a simple example demonstrating how to use the GridView control to display a list of products.

1. Define the Product Class


    public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public decimal Price { get; set; }
    }
    

2. Create a Method to Get Sample Data


    public List<Product> GetProducts()
    {
        return new List<Product>
        {
            new Product { ProductID = 1, ProductName = `Product A`, Price = 10.00M },
            new Product { ProductID = 2, ProductName = `Product B`, Price = 20.00M },
            new Product { ProductID = 3, ProductName = `Product C`, Price = 30.00M }
        };
    }
    

3. Add the GridView Control to the ASPX Page


    <asp:GridView ID=`gvProducts` runat=`server` AutoGenerateColumns=`false` OnPageIndexChanging=`gvProducts_PageIndexChanging`>
        <Columns>
            <asp:BoundField DataField=`ProductID` HeaderText=`ID` />
            <asp:BoundField DataField=`ProductName` HeaderText=`Product Name` />
            <asp:BoundField DataField=`Price` HeaderText=`Price` />
        </Columns>
    </asp:GridView>
    

4. Code-Behind to Bind Data


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        gvProducts.DataSource = GetProducts();
        gvProducts.DataBind();
    }
    

5. Implement Paging

To enable paging, you need to handle the PageIndexChanging event:


    protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProducts.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    

Conclusion

The GridView control is an essential component in ASP.NET Web Forms for displaying and managing data in a tabular format. Its built-in features for sorting, paging, and editing make it a versatile tool for developers. By following the example provided, you can easily implement a GridView control in your web applications to present data effectively and interactively.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.