How to use sorting in GridView


GridView Sorting



The GridView is an asp.net web server control that allows us to display the values of a data source in a table. GridView server control provides built-in sorting functionality without requiring any coding.




Asp.net developers can enable the GridView default sorting behavior by setting its AllowSorting property value to True. Sorting-enabled GridView server control renders 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 gets the sort expression associated with the column or columns being sorted.




When users click a LinkButton control in the 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 been executed, the GridView’s Sorted event is raised. Sorted events 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 throws the NotSupportedException exception.




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




We can disable sorting for individual fields such as 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 events allow us to customize the sort expression that is passed to the data source control. The 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.




Asp.net developers can write their own sorting logic for those data source controls that do not support sorting.




GridView EnableSortingAndPagingCallbacks property value indicates whether client-side callbacks are used for sorting and paging operations. SortDierection property helps 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. The OnSorted method raises the Sorted event. The OnSorting method raises the Sorting event. The Sorts method sorts the GridView data based on the specified sort expression and direction.




The following asp.net c# example code demonstrates to us how we can sort GridView data.




GridViewSortingExample.aspx



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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net GridView Sorting example: how to sort GridView Data</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">Sorting In GridView</h2>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT TOP 6 ProductID, ProductName, UnitPrice FROM Products"
>
</asp:SqlDataSource>
<asp:GridView
ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
BackColor="AliceBlue"
ForeColor="Goldenrod"
BorderColor="LightSalmon"
BorderStyle="Groove"
AllowSorting="true"
>
<Columns>
<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" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>