It would often be useful to be able to sort by more than one column in a DataGrid. .Net has no built-in way to sort by multiple columns, but this article shows one way that some extra code can make this "multi-sorting" possible.
What the user sees
Consider a DataGrid with multiple columns. Using the code presented here, when the header text in any column is clicked on, that column is shifted to the left and the DataGrid is sorted by that column in ascending order. If that same column is clicked on again, the DataGrid is sorted by that column in descending order. When the header text in another column is clicked on, that column then moves to the second-to-the-left position, and the DataGrid is sorted, first by the left-most column and second by the next column. This can be repeated with any number of columns in the DataGrid. Each of the columns that determine the sort order has an "x" added to the header text. Clicking on the "x" will remove that column from the sort order and the column will return to its original sort order. This facilitates sorting (and "un-sorting") the columns of a DataGrid easily in all possible configurations.
While most of the work is done in the MultiOptions class, there are three places in the code behind where code has to be added to make the multi-column sort work.
- When the page loads the MultiOptions class is initialized.
- When the DataGrid is created, the field names are passed to the MultiOptions class so that it can keep track of the field names and original column positions. The order the columns appear in the DataGrid is determined when the grid is created so the MultiOptions class is called when each column is created to get its field name and header text.
- When the DataView is sorted, a call is made to the MultiOptions class to get the SortExpression.
The sorting state is persisted through the querystring. The querystring has three variables in the querystring: the original column number, the direction of the selected column, the column number and sort direction of each of the columns in the sort order.
The sort order is created in the constructor of the class, which is called every time the page loads, by looking at the persisted sort order, the column that was selected, the direction, and whether the column should be deleted. There are three possible results that can occur in the constructor:
- If a column that is not in the sort order is selected, it is added to the sort order.
- If a column that is in the sort order is selected, the column is sorted in the opposite direction.
- If the user clicks on the "x" instead of the header text, the column is removed from the sort order.
When the DataGrid is created calling the SetFields method, the sort order in created in the constructor is used to determine the order the columns will appear in the grid.
The sort order that is modified in SetFields is used to create the header text link that determines the sort order and direction when the link is clicked.
The list of columns and sort direction used for sorting the DataView is generated based on the sort order in the querystring.
View Live Demo
View multisort.aspx Source
View multisort.aspx.cs Source
View MultiOptions.cs Source
View XML Source