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

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”
        Else
           OleDbCommand1.Parameters("@Shipped").Value = _
             DropDownList1.SelectedItem.Text
        End If


        If DropDownList2.SelectedIndex = "0" Then
          OleDbCommand1.Parameters("@Invoiced").Value = “All”
        Else
          OleDbCommand1.Parameters("@Status").Value =  _
          DropDownList2.SelectedItem.Text
        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"
            Else
                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


        dataGrid.DataBind()


    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.

(OnSortCommand="SortCommand_OnClick")

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" 


OnSortCommand="SortCommand_OnClick"


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


BackColor="LightCyan" BorderWidth="1px" 


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


AutoGenerateColumns="False">

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
Comment:
cff
Title: thank you   
Name: Klint
Date: 2010-12-16 2:20:39 PM
Comment:
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
Comment:
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
Comment:
very useful.....i was struggling for the bidirectional sorting for a long time.....it worked fine...thanks a ton
Title: Bidirectional datagrid sorting   
Name: Thenu
Date: 2006-03-15 6:31:53 AM
Comment:
This article was quiet useful.Thanks
Title: sql server error   
Name: Sonalee
Date: 2005-05-04 12:32:47 AM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
eng_mahmoud_tahoon@hotmail.com
Thank you

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-08-22 6:41:06 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search