How to Use ASP.NET Web Forms GridView for Sorting and Paging?

1. Start by making a page in your application called YourPage.aspx
To implement paging and sorting in an ASP.NET GridView, you must do the following actions:

  1. Sort and Page: Sorting and paging can be made available by setting the AllowSorting and AllowPaging attributes to True.
  2. Set Page Size: You can specify how many records are shown on each page by using the PageSize property.
    Handle Sorting and Paging Events: In order to control the sorting and paging functionality, you must handle the Sorting and PageIndexChanging events.
  3. Bind Data: To keep sorting and paging in place, make sure the GridView is tied to a data source on each postback.

Sample Programming

This is a comprehensive illustration of an ASP.NET GridView with paging and sorting turned on:

2. Now In Your YourPage.cs Page Add Method Of Sorting And Paging

Key Points of Sorting and Paging in Code-Behind

Sorting Event Handler (MDetails_Sorting)

  • Event Trigger: This method is triggered when a column header is clicked to sort the GridView.
  • Retrieve Data: Retrieve the data from ViewState.
  • Sort Data: Sort the data based on the clicked column (e., SortExpression) and the sort direction.
  • Rebind Data: Binds the sorted data back to the GridView.

2. Paging Event Handler (MDetails_PageIndexChanging)

  • Event Trigger: This method is triggered when a different page is selected in the GridView.
  • Set Page Index: Updates the PageIndex of the GridView to the new page index.
  • Rebind Data: Calls BindGridView() to rebind the data and refresh the GridView with the new page.

3. Get Sort Direction (GetSortDirection)

  • Purpose: Determines the sort direction (ascending or descending) based on the current column.
  • State Management: Uses ViewState to store and retrieve the current sort direction and expression.

4. Bind Data (BindGridView)

    • Purpose: Binds the GridView with data.
    • Data Retrieval: Calls GetFamilyDetails() to fetch the data.
    • State Management: Stores the data in ViewState for later use (sorting, paging).
    • Bind Data: Binds the data to the GridView control.

ASP.NET 8.0.4 Hosting Recommendation

ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts