To implement multiple-column sorting and provide visual indication of the sort direction, we need to override the two methods that invoke the Sorting and RowCreated events.
The first method is OnSorting, which is called whenever a column is sorted. We simply check to see if the custom property AllowMultiColumnSorting is set to true and, if so, call upon a custom method that gives us the multiple-column sort expression.
It is worth noting that we don't need to do anything special to remember the sort expression between page postbacks. This is built into the GridView implementation and its SortExpression property stores the last-used sort expression. Likewise, just by trapping the OnSorting method and changing the SortExpression in the event argument, we're letting GridView fire the Sorting event with the new multiple-column sort expression and passing it to the DataView exposed by the data source control.
protected override void OnSorting(GridViewSortEventArgs e)
e.SortExpression = GetSortExpression(e);
Next, we override the virtual method OnRowCreated, and set up a visual indicator in the header row for the sort direction.
protected override void OnRowCreated(GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.Header)
if (SortExpression != String.Empty)
Note that in the above-mentioned ASP.NET 1.x article, we had to implement this logic within the ItemDataBound and SortCommand event handlers at the page level. Here, due to the added capabilities of the GridView control, we are able to shift the logic into the control's implementation. This enables a no-code implementation (other than declarative syntax) at the page level and simplifies reuse.