Results 1 to 3 of 3

Thread: sorting a datagrid Ascending, descending

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    sorting a datagrid Ascending, descending

    I want to sort datagrid columns ascending or descending. They always sort ascending.

    How can I do descending.

    This is the code I have in the Sort Command event.

    VB Code:
    1. Private Sub dgrResults_SortCommand1(ByVal source As Object, ByVal e As _
    2. System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgrResults.SortCommand
    3.  
    4.         Dim dstStudents As DataSet = PopulateGrid()
    5.  
    6.         ' Create a DataView from the Dataset.
    7.         Dim dvwStudents As New DataView(dstStudents.Tables(0))
    8.         Try
    9.  
    10.             ' The DataView provides an easy way to sort. Simply set the
    11.             ' Sort property with the name of the field to sort by.
    12.             dvwStudents.Sort = e.SortExpression
    13.  
    14.             ' Rebind the data source and specify that it should be sorted
    15.             ' by the field specified in the SortExpression property.
    16.  
    17.             dgrResults.DataSource = dvwStudents
    18.             dgrResults.DataBind()
    19.         Catch ex As Exception
    20.             Response.Redirect(clsLib.RedirectTo(ex.Message, "Search.aspx"))
    21.         End Try
    22.  
    23.     End Sub


    The PopulateGrid function returns a dataset of data.


    Things I do when I am bored: DotNetable

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    just add DESC after the sortexpression ie.
    VB Code:
    1. dvwStudents.Sort = e.SortExpression & " DESC"
    I think that should work anyway.

    Here's an example from a datagrid that allows you to click a column heading to sort by that column. Clicking on that column heading again then sorts in the other direction.
    VB Code:
    1. Sub Sort_Grid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
    2.  
    3.         If Session("SortColumn") = e.SortExpression.ToString() Then
    4.             'Same sort expression used as last time so reverse the order
    5.             If Session("SortOrder") = "" Then
    6.                 Session("SortOrder") = " DESC"
    7.             Else
    8.                 Session("SortOrder") = ""
    9.             End If
    10.         End If
    11.         Session("SortColumn") = e.SortExpression.ToString()
    12.         Session("SortExpression") = e.SortExpression.ToString() + Session("SortOrder")
    13.         gridLeaderBoard.DataSource = createDataSource()
    14.         gridLeaderBoard.DataBind()
    15.  
    16.     End Sub
    then in the createDataSource function...
    VB Code:
    1. dv.Sort = Session("SortExpression")

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Thanks again

    FishCake helped me out with a similar sorting when paging problem here


    Things I do when I am bored: DotNetable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width