Multiple Column Sorting with a DataGrid
page 1 of 1
Published: 20 Sep 2003
Unedited - Community Contributed
Abstract
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.
by Daniel Olson
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 13173/ 21

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.

Code Behind
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.

MultiOptions class
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 Constructor
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.

SetFields method
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.

GetHeaderText method
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.

GetSortExpression method
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

Download code



User Comments

Title: Sort By Multiple Columns   
Name: venkat
Date: 2007-05-18 5:16:23 PM
Comment:
DataTable.Select Method (String, String)

string sortOrder = "FirstName,LastName DESC";

DataTable.Select Method (query, sortOrder );

try this.
nvr
Title: Multiple Column Sorting   
Name: Uday Bhaskar
Date: 2007-03-26 2:43:29 AM
Comment:
Your article little bit complecated
Title: MultiSort with Paging?   
Name: sp
Date: 2006-08-28 7:59:50 AM
Comment:
i didnt get give clearly
Title: Multiple column sorting   
Name: Gauri
Date: 2006-06-21 2:32:54 AM
Comment:
2 much complecated explanation.
Title: Multiple Column Sorting with a DataGrid   
Name: Harish Sharma
Date: 2005-03-17 10:33:45 AM
Comment:
Your article is not to the extent, then also it is bit helpful, using it someone will be able to do any more thing with datagrid
Title: MultiSort with Paging?   
Name: NK
Date: 2004-11-10 3:14:06 AM
Comment:
Your article is pretty neat.
How can I make it your code work with paging too?






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 2:34:17 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search