Sorting Data in a GridView Web Server Control
The GridView control provides built-in sorting functionality without requiring any coding. You can further customize the sort functionality of the GridView control by using custom SortExpression property values for columns as well as by using the Sorting and Sorted events.
The GridView control does not perform its own sorting of columns, but rather relies on the data source control to perform sorting on its behalf. The control provides the user interface (UI) for sorting, such as LinkButton controls displayed at the top of each column of the grid. However, the GridView control relies on the data-sorting capabilities of the data source control to which it is bound.
If the bound data source control can sort data, then the GridView control can interact with the data source control and request sorted data by passing a SortExpression to the data source when data is selected. Not all data source controls support sorting; for example, the XmlDataSource control does not. If the data source control supports sorting, however, the GridView can take advantage of it. The following list describes data source controls and the configuration that is needed to support sorting:
GridView Sorting Process
You can enable the default sorting behavior in the GridView control by setting its AllowSorting property to true. Setting this property to true causes the GridView control to render a LinkButton control in the column headers. The control also implicitly sets the SortExpression property of each column to the name of the data field to which it is bound. For example, if the grid contains a column that displays the City column of the Employees table in the Northwind sample database, the SortExpression property of that column will be set to City.
At run time, users can click the LinkButton control in a column heading to sort by that column. Clicking the link causes the page to perform a postback and raises the GridView control's Sorting event. The sort expression — by default, the name of the data column — is passed as part of the event arguments. The default behavior for the Sorting event is that the GridView control passes the sort expression to the data source control. The data source control executes its selection query or method, including the sort parameters passed by the grid.
After the query has executed, the grid's Sorted event is raised. This event allows you to perform post-query logic, such as displaying a status message. Finally, the data source control rebinds the GridView control to the results of the resorted query.
The GridView control does not check whether the data source control supports sorting; it always passes the sort expression to the data source. If the data source control does not support sorting and a sorting operation is performed in the GridView control, the GridView control throws the NotSupportedException exception. You can catch this exception in a handler for the Sorting event and check the data source to determine whether sorting is supported, or by using your own sorting logic.
Setting the AllowSorting property of the control enables sorting for columns by default. You can disable sorting for individual fields (for example, the BoundField or TemplateField field) by setting the SortExpression property of the individual column to an empty string ("").
You can add styles to the column that the data is sorted by, and you can display arrows in the column heading to indicate whether sorting is ascending or descending. You can also change the style of the column that the data is sorted by based on the sort direction. The TableItemStyle properties enable you to add style settings to the GridView columns, as listed in the following table.
When this style is set, an arrow that points up is displayed in the column heading when the data in that column is sorted in ascending order.
When this style is set, the style is applied to cells when the data in that column is sorted in ascending order.
When this style is set, an arrow that points down is displayed in the column heading when the data in that column is sorted in descending order.
When this style is set, the style is applied to cells when the data in that column is sorted in descending order.
If the default sort behavior is not adequate for your requirements, you can customize the grid's sorting behavior. The basic technique for custom sorting is to handle the Sorting event. In the handler, you can do the following:
Customize the sort expression that is passed to the data source control. By default, the sort expression is the name of a single column. You can modify the sort expression in the event handler. For example, if you want to sort by two columns, you can create a sort expression that includes both. You can then pass the modified sort expression to the data source control. For more information, see the GridViewSortEventArgs.SortExpression property.
Create your own sorting logic. For example, if you are working with a data source that does not support sorting, you can perform the sort in your own code and then bind the grid to the sorted data.