How to use sorting in GridView

GridView Sorting
GridView is an web server control that allow us to display the values of a data source in a table. gridview server control provides built in sorting functionality without requiring any coding. developers can enable the gridview default sorting behavior by setting its AllowSorting property value to True. sorting enabled gridview server control render a LinkButton control for each column in the column headers section. it also implicitly set the SortExpression property of each column to the name of the data field to which it is bound. SortExpression property get the sort expression associated with the column or columns being sorted.

when users click a LinkButton control in column heading, it causes a postback and raises the gridview Sorting event. SortExpression is passed as part of the Sorting event arguments. gridview control passes the sort expression to the data source control. data source control executes its selection query or method including sort parameters. after the query has executed, the gridview's Sorted event is raised. Sorted event allow us to perform post query logic such as displaying a status message. finally the data source control rebinds the gridview to the results of the resorted query.

gridview does not check whether data source control supports sorting, gridview always passes sort expression to the data source. if the data source control does not support sorting, the gridview control throw the NotSupportedException exception.

XmlDataSource control does not support sorting. gridview control can take advantage from the data source control those support sorting. SqlDataSource and AccessDataSource controls can sort data if the DataSourceMode property value is set to DataSet or SortParameterName property value is set to either DataSet or DataReader.

we can disable sorting for individual fields such as a BoundField or TemplateField by setting its SortExpression property value to an empty string.

gridview SortedAscendingHeaderStyle, SortedAscendingCellStyle, SortedDescendingHeaderStyle and SortedDescendingCellStyle properties allow us to apply css styles to the column that is sorted. we also can display arrows in the sorted column heading to indicate whether sorting is ascending or descending.

we can also customize the gridview default sorting behavior by its Sorting event. Sorting event allow us to customize the sort expression that is passed to the data source control. default sort expression is the name of a single column. using this event we can sort gridview data by two columns, we just need to create a sort expression that includes both columns. developers can write their own sorting logic for those data source controls that does not support sorting.

gridview EnableSortingAndPagingCallbacks property value indicate whether client side callbacks are used for sorting and paging operations. SortDierection property help us to determine whether the column being sorted is sorted in ascending or descending order.

gridview GetCallbackScript method creates the callback script for a button that performs a sorting operation. OnSorted method raises the Sorted event. OnSorting method raises the Sorting event. Sorts method sorts the gridview data based on the specified sort expression and direction.

the following c# example code demonstrate us how can we sort gridview data.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">


<html xmlns="">
<head id="Head1" runat="server">
    <title> GridView Sorting example: how to sort GridView Data</title>
    <form id="form1" runat="server">
        <h2 style="color:Teal">Sorting In GridView</h2>
             ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
             SelectCommand="SELECT TOP 6 ProductID, ProductName, UnitPrice FROM Products"
                <asp:BoundField SortExpression="ProductID" HeaderText="Product ID" DataField="ProductID" />
                <asp:BoundField SortExpression="ProductName" HeaderText="Product Name" DataField="ProductName" />
                <asp:BoundField SortExpression="UnitPrice" HeaderText="Unit Price" DataField="UnitPrice" />
Related examples