[ASP.NET] GridView – Sorting and Paging

The “GridView” control has a built-in support of sorting and pagaing. They are very powerful features but needs to be used cautiously.

1. Sorting

The GridView sorting reorders the results by clicking a column header.

  • Set the “AllowSorting” property of the “GridView” to true
  • Set the “SortExpression” property of each column (usually the field name is set)
public class GridView
{
  public bool AllowSorting { get; set; }
  public string SortExpression { get; } // sorted by what?
  public SortDirection SortDirection { get; }

  public event GridViewSortEventHandler Sorting;
  public event EventHandler Sorted;
}

Not all DataSource controls support sorting. If you use the “SqlDataSource”, the “DataSourceMode” should be set to “DataSet”, not “DataReader”.

2. Sorting and Selection

Sorting reorders the rows but by default the “SelectedIndex” value is not changed. Therefore after sorting the selection will point to the different value (but to the same location). If you want to persist the selection, simply set the “EnablePersistedSelection” property to true.

<asp:GridView ID="productSubCategoryGV" runat="server"
  DataSourceID="productSubCategorySqlDS"
  AllowSorting="True"
  AutoGenerateColumns="False"
  DataKeyNames="ProductSubcategoryID"
  EnablePersistedSelection="True">
  <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="ProductSubcategoryID" HeaderText="ID" SortExpression="ProductSubcategoryID" />
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
  </Columns>
  <SelectedRowStyle BackColor="#99CCFF" />
</asp:GridView>

3. Paging

public class GridView
{
  public bool AllowPaging { get; set; }
  public int PageIndex { get; set; }
  public virtual int PageCount { get; }
  public virtual int PageSize { get; set; }

  public event GridViewPageEventHandler PageIndexChanging;
  public event EventHandler PageIndexChanged;
}
  • PageIndexChanging: just before the PageIndex is changed
  • PageIndexChanged: just after the PageIndex is changed
<asp:GridView ID="productSubCategoryGV" runat="server"
  DataSourceID="productSubCategorySqlDS"
  AllowPaging="True"
  AutoGenerateColumns="False"
  DataKeyNames="ProductSubcategoryID"
  EnablePersistedSelection="True"
  PageSize="5">
  <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="ProductSubcategoryID" HeaderText="ID" />
    <asp:BoundField DataField="Name" HeaderText="Name" />
  </Columns>
  <SelectedRowStyle BackColor="#99CCFF" />
</asp:GridView>

4. Paging and Performance

The GridView paging is very easy to use but it is not optimized. Every time a new page is requested, the full resultset is returned from the database. To enhance the performance of the paging, you might want to cache the data or provide the custom logic to request the data just for the requested page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s