Introduction
The Gridview is the most used control to display data in a tabular format. By default it supports major features like sorting, paging. However it does not support multiple sorting. In this article I will explain how you can create a multiple sorting feature in a Gridview control.

The GridView Control
I have been a datagrid control user since a long time. However when 2.0 released I switched over to the Gridview control. I was a little disappointed to see that the Gridview control does not support multiple sorting by default. I thought that Microsoft would add that feature after seeing how developers extending the datagrid control. Luckily creating such a feature is very easy - with some few lines of code you are ready to go.
Multiple Sorting
To use multiple sorting, I created a ListDictionary object, which would hold the sort-expressions. I kept them in the viewstate, so that they could be persisted during postbacks.
Getting started
The majority of the work is done in the GridView.Sorting event, which is called whenever you click the header link in the Gridview. The code that I have used in that event is showed below.
GridView.Sorting
So what's happening in this code? Very simple! First I need to check whether the sort-expression is already added to the ListDictionary object or not. Of course we do not want to add multiple keys into the ListDictionary object. To check that I simply used the Contains function, which returns a boolean value indicating whether the key already exists or not. If they key does not exist, then I add it to the ListDictionary and replace the current expression "ASCENDING" with "ASC" and "DESCENDING" with "DESC". However if the key already exists, then we need to check, whether the user wants to sort Ascending, Descending or whether he wants to cancel sorting at all. This is simply done via the If/Else statement. Once this is done, we store the m_ldSortExpressions object to the SortExpression property, which will store it into the viewstate. After that we simple rebind the data, which will use the SortExpressions and display the data.
Binding
Usually you would use the built sortexpression, pass it to a stored-procedure and return the sorted result. To get the stored expressions from the ListDictionary, I used a simple loop which stored keys and values to into a StringBuilder object. Once the loop is finished, I used the built sort-expression to sort a dataview; below is the loop, which I have used.
Summary
In this article, you have seen how to use the Gridview to manually sort the Gridview with multiple columns in both directions.
About Sonu Kapoor
 |
Sonu Kapoor is the creator of DotNetSlackers. Originally started in 2005, DotNetSlackers is a website to help developers stay up-to-date with the latest developments in .NET. During the sites’ youth DotNetSlackers focused solely on .NET news aggregation; however, over the years DotNetSlackers has gr...
This author has published 3 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Multiple child views with RadGridView for WinForms
read more
Gaia Ajax 3.6 Beta Released! Free download of new Ajax GridView, 35++ Ajax Components and 100++ New Samples
read more
WPF Release History : Q1 2009 SP2 (version 2009.1.526)
read more
Silverlight Release History : Q2 2009 (version 2009.2.701)
read more
Silverlight Release History : Q1 2009 SP1 (version 2009.1.413)
read more
Silverlight Release History : Q1 2009 SP2 (version 2009.1.526)
read more
WPF Release History : Q1 2009 (version 2009.1.312)
read more
RadControls for WPF/Silverlight Q3 Beta 2 release is live!
read more
WPF Release History : Q1 2009 SP1 (version 2009.1.413)
read more
Silverlight Release History : Q1 2009 (version 2009.1.312)
read more
|
|
Please login to rate or to leave a comment.