Friday, December 10, 2010

Optimized Paging and Sorting in ASP.NET GridView


Paging and sorting are most commonly used features of ASP.NET GridView. And it is very easy to use/implement these features in GridView with small chunk of lines. In this article I am going to demonstrate what are the performance drawbacks of using conventional way to page and sort your GridView and then I will demonstrate 'An Optimized way to implement Paging and Sorting'.

What are conventional steps for Paging and Sorting?

Usually we perform the following steps to enable paging and sorting in our GridView.
1. Set AllowPaging and AllowSorting Properties of GridView to True to enable paging and sorting respectively e.g
view sourceprint?
1.<asp:GridView ID="GridView1" runat="server" AllowPaging="true" AllowSorting="true"  >
2. </asp:GridView>
2. Set the PageSize property to mention how many records will be display on each page.
3. Set the SortExpression property of each column. By default each Data Bound columns has the bounded column name as default value for the SortExpression property.
4. Handle PageIndexChanging and Sorting Events of GridView to respond to paging and sorting actions respectively, like so:
view sourceprint?
01.<asp:GridView ID="GridView1" runat="server" AllowPaging="true"
02.            AllowSorting="true" onpageindexchanging="GridView1_PageIndexChanging"
03.            onsorting="GridView1_Sorting"  >
04.        </asp:GridView>
05. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
08.protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
5. Put some logic in the event handlers to do their jobs
5a. In the PageIndexChanging Event Handler method, we usually get the data from database or somewhere from the Cache and rebind our Grid with that data. After rebinding we change the PageIndex property of the GridView to a new page index to display the page that was selected by the user.
view sourceprint?
1.protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
2. {
3.     GridView1.DataSource = GetData(); // GetData() is a method that will get Data from Database/Cache/Session to display in Grid.
4.     GridView1.DataBind();
5.     GridView1.PageIndex = e.NewPageIndex;
6. }
5b. In the Sorting event handler method, we get the sorted data according to the sort expression from our data source (data source could be database/cache/session etc) and then rebind the Grid to display the sorted records.
And that's it.


In conventional way of paging and sorting we get complete set of data instead of getting only the portion of data that is required to display on current/requested page. As you can see on each pageIndexChanging call we are getting all the data from our data source and then binding it to the GridView. Ideally we should get only the data that we need to display on the requested page.

Hmmm...Sounds good but HOW??

The question that may arise in your mind could be "It seems good in theory that we should only get the required data, but practically if we bind only one page of data with GridView then it would assume that this is the only data that it needs to display. So how does the GridView even display page numbers and total records count? It is a genuine question, so let's try to answer!

An Optimized Way to implement Paging and Sorting

As in the start of this article, we discuss the conventional 5 steps to implement paging and sorting in ASP.NET GridView . In this solution we will use the first 3 steps as described above, and perform the 4th and 5th steps by ourselves. We will use an ObjectDataSource that will perform these steps for us in an optimized way.

Action Method in Mvc

In this section, you will learn about the action method of controller class. All the public methods of a Controller class are called Act...