Sorting a Gridview with multiple Columns

Published: 17 Sep 2006
By: Sonu Kapoor

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.

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

   1:  Protected Sub GridView1_Sorting(ByVal sender As Object, _ 
ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
   2:    m_ldSortExpression = SortExpressions
   3:    If Not m_ldSortExpression.Contains(e.SortExpression) Then
   4:      m_ldSortExpression.Add(e.SortExpression, e.SortDirection.ToString.Replace("Ascending", "ASC").Replace("Descending", "DESC"))
   5:    Else
   6:      ' Get sort direction
   7:      Dim strSortDirection As String = m_ldSortExpression.Item(e.SortExpression)
   8:      ' Was it ascending?
   9:      If strSortDirection = "ASC" Then
  10:        ' Yes, so sort in desc
  11:        m_ldSortExpression.Item(e.SortExpression) = "DESC"
  12:      ElseIf strSortDirection = "DESC" Then
  13:        ' it is descending
  14:        ' remove the sort order
  15:        m_ldSortExpression.Remove(e.SortExpression)
  16:      End If
  17:    End If
  18:    SortExpressions = m_ldSortExpression
  19:    BindData()
  20:  End Sub

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. Ofcourse 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.

   1:  If SortExpressions.Count > 0 Then
   2:    Dim myKeys(SortExpressions.Count) As String
   3:    SortExpressions.Keys.CopyTo(myKeys, 0)
   4:    For i As Integer = 0 To SortExpressions.Count - 1
   5:      sbSortExpression.Append(myKeys(i))
   6:      sbSortExpression.Append(" ")
   7:      sbSortExpression.Append(SortExpressions(myKeys(i)))
   8:      If i <> SortExpressions.Count - 1 Then
   9:        sbSortExpression.Append(", ")
  10:      End If
  11:    Next
  12:    lblSortExpression.Text = sbSortExpression.ToString
  13:    ' usually we would send that sort-expression now to SQL
  14:    dv.Sort = sbSortExpression.ToString
  15:  Else
  16:    lblSortExpression.Text = String.Empty
  17:  End If

Summary

In this article, you have seen how to use the Gridview to manually sort the Gridview with multiple columns in both directions.

Download

GridViewMultipleSorting (VS2005 Project)

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

View complete profile

Top Articles in this category

Persisting Selection in ASP.NET Grid Controls While Paging
How to persist selection correctly in a grid control (DataGrid, GridView, and ListView controls) when paging is enabled.

A first look at the Dynamic Data Engine—the DynamicGridView Control
Dino Esposito introduces the ASP.NET DynamicGridView Control.

Custom GridView with Paging and Filtering
A custom control derived from the GridView that implements filtering, custom Top pager and custom Bottom pager.

Top
 
 
 

Please login to rate or to leave a comment.

Product Spotlight