Results 1 to 3 of 3

Thread: problems with sort when paging a datagrid

  1. #1

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

    problems with sort when paging a datagrid

    I have a datagrid with paging and sorting enabled.

    When on page 1 I sort the column and all looks OK. When I select another page e.g. page 3 the columns are not sorted on the same column.

    I am told this is a known problem.


    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
    This is easy enough to handle. Store the sortExpression somewhere and use it whenever you need to rebind the datagrid (page index changed, sort grid etc). Here's an example of what i mean, sorry about the large amount of code but i thought it might help for you to see an example.
    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
    17.  
    18.     Sub Grid_changed(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
    19.         gridLeaderBoard.CurrentPageIndex = e.NewPageIndex
    20.  
    21.         gridLeaderBoard.DataSource = createDataSource()
    22.         gridLeaderBoard.DataBind()
    23.     End Sub
    24.  
    25.     Function createDataSource() As ICollection
    26.         Dim dt As New DataTable
    27.         Dim dr As DataRow
    28.         Dim cmdSelect As SqlCommand
    29.         Dim dtrReader As SqlDataReader
    30.  
    31.         dt.Columns.Add(New DataColumn("Position", GetType(Short)))
    32.         dt.Columns.Add(New DataColumn("UserName", GetType(String)))
    33.         dt.Columns.Add(New DataColumn("played", GetType(Short)))
    34.         dt.Columns.Add(New DataColumn("points", GetType(Short)))
    35.         dt.Columns.Add(New DataColumn("Average", GetType(Single)))
    36.         dt.Columns.Add(New DataColumn("highest", GetType(Short)))
    37.  
    38.         club256.connection.connect(Application("SQLServerAddress"), con256club)
    39.  
    40.         cmdSelect = New SqlCommand("up_sel_leaderboard2", con256club)
    41.         cmdSelect.CommandType = CommandType.StoredProcedure
    42.  
    43.         dtrReader = cmdSelect.ExecuteReader
    44.  
    45.         Dim i As Short = 1
    46.         While dtrReader.Read
    47.             dr = dt.NewRow
    48.             dr(0) = i
    49.             dr(1) = dtrReader("screen_name")
    50.             dr(2) = dtrReader("played")
    51.             dr(3) = dtrReader("totalscore")
    52.             dr(4) = Decimal.Round(dtrReader("averagescore"), 3)
    53.             dr(5) = dtrReader("highest")
    54.             dt.Rows.Add(dr)
    55.             i += 1
    56.         End While
    57.         dtrReader.Close()
    58.         cmdSelect.Dispose()
    59.         club256.connection.Disconnect(Application("SQLServerAdress"), con256club)
    60.         Dim dv As New DataView(dt)
    61.         dv.Sort = Session("SortExpression")
    62.  
    63.         Return dv
    64.     End Function

    EDIT : I should just have combined this answer with you're other datagrid sort post really.

  3. #3

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

    I had got the sortexpression working OK I hadn't found where to define Ascending or descending.

    It really helped


    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