CodeSnip: Bidirectional Sorting of a DataGrid in VB.NET
page 1 of 1
Published: 10 Nov 2004
Unedited - Community Contributed
This article shows how to, in few simple steps, implement bidirectional sorting for DataGrids while maintaining the filter status (criteria).
by Sameer Lal
Average Rating: 
Views (Total / Last 10 Days): 9308/ 9

This tip shows you how to sort a DataGrid ascending and descending for any column while retaining the filter criteria.

Create an OleDbCommand that has a stored procedure from MS Access, e.g.:

"Select * From tblOrder Where (([tblOrder].[Shipped]=[@Shipped] Or" &_ 
"[@Shipped]=”All”) And ([tblOrder].[Invoiced]=[@Invoiced] Or" &_ 
"[@Invoiced]=”All”) And ([tblOrder].[OrderDate]>=" &_ 
"[@SDate] And [tblOrder].[OrderDate]<=(DateAdd("d",1,[@EDate]))))" &_
"Order By [tblOrder].[OrderNumber] Desc;"

Add these parameters to the parameter collection for the command.

The other actions, such as connecting to the Access database, filling the DataGrid on Page.Load, and using the other controls on the form page, are not discussed here.

Function CreateDataTable() As DataTable

        OleDbCommand1.Parameters("@SDate").Value = TextBox1.Text
        OleDbCommand1.Parameters("@EDate").Value = TextBox2.Text

        If DropDownList1.SelectedIndex = "0" Then
           OleDbCommand1.Parameters("@Shipped").Value = “All”
           OleDbCommand1.Parameters("@Shipped").Value = _
        End If

        If DropDownList2.SelectedIndex = "0" Then
          OleDbCommand1.Parameters("@Invoiced").Value = “All”
          OleDbCommand1.Parameters("@Status").Value =  _
        End If

        Dim adp As New OleDb.OleDbDataAdapter(OleDbCommand1)
        Dim ds As New DataSet

        adp.Fill(ds, "tblOrder")

        Return ds.Tables("tblOrder")

    End Function

  Sub SortCommand_OnClick(ByVal Source As Object, _
      ByVal E As DataGridSortCommandEventArgs)

        Dim dataGrid As DataGrid = Source
        Dim strSort = dataGrid.Attributes("SortExpression")
        Dim strASC = dataGrid.Attributes("SortASC")

        dataGrid.Attributes("SortExpression") = E.SortExpression
        dataGrid.Attributes("SortASC") = "Yes"

        If E.SortExpression = strSort Then

            If strASC = "Yes" Then
                dataGrid.Attributes("SortASC") = "No"
                dataGrid.Attributes("SortASC") = "Yes"
            End If

        End If

        Dim DTable As DataTable = CreateDataTable()
        Dim DView As DataView = New DataView(DTable)
        DView.Sort = dataGrid.Attributes("SortExpression")

        If dataGrid.Attributes("SortASC") = "No" Then
            DView.Sort &= " DESC"
        End If

        dataGrid.DataSource = DView


    End Sub

Please do not forget to go to the Property Builder for the DataGrid and, on the General tab, check the box to allow sorting. Uncheck the auto generate column property and mark all the columns as bound columns with the sorting expression as the corresponding field.  Also, you will need to change to the HTML view and add the following code for sorting.


Your HTML code will look something like this (note the OnSortCommand property):

<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 10px; 

POSITION: absolute; TOP: 144px"runat="server" 


 AllowSorting="True" Font-Size="X-Small" CellPadding="4" 

BackColor="LightCyan" BorderWidth="1px" 

BorderStyle="None" BorderColor="#3366CC" 


I hope you find this method of bidirectional sorting while maintaining the criteria state easy to use.

User Comments

Title: fff   
Name: fff
Date: 2012-09-18 2:54:02 AM
Title: thank you   
Name: Klint
Date: 2010-12-16 2:20:39 PM
I have looked at sample after sample online, and this detailed listing laid out the logic in a simple to understand manner. Thank you.
Title: It is very good to know about our needed search but we nedd appropriate example   
Name: manoj kumar nayak,orrissa-757086
Date: 2006-06-01 2:44:05 AM
It is very good to know about our needed search but we nedd appropriate example
Title: gr8 work   
Name: Anand
Date: 2006-04-07 8:08:47 AM
very useful.....i was struggling for the bidirectional sorting for a long worked fine...thanks a ton
Title: Bidirectional datagrid sorting   
Name: Thenu
Date: 2006-03-15 6:31:53 AM
This article was quiet useful.Thanks
Title: sql server error   
Name: Sonalee
Date: 2005-05-04 12:32:47 AM
I am trying to access an ASP.NET application.i copied it to my machine from another machine in my network.i have set up a virtual directory for the application but on trying to acces it via localhost it saya:SQL Server does not exist or access denied.
Title: Good......   
Name: Joe
Date: 2005-01-11 11:39:32 AM
this article is really good ... i was trying to sort the data that was added static and not related to db... it really made my job east
Title: awesome !!!   
Name: [soksa]icy
Date: 2004-12-01 7:14:11 PM
never thought of this. very very clever!!! good job and thanks a mill (F)
Title: Help Me I Can't connect to SQL-Server Databases   
Name: Mahmoud Tahoon
Date: 2004-11-12 7:24:57 PM
Hellp Mr.Sameer
would you please help me i got a serious problem here
i already have a microsoft sql-server 2000 copy
and when i try to connect to any databases on it i always have the same error everytime :
SQL Server does not exist or access denied
Please help me and send me at my email
Thank you

Product Spotlight
Product Spotlight 

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

©Copyright 1998-2024  |  Page Processed at 2024-04-18 6:20:02 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search