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.
Printable View
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.
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:
Sub Sort_Grid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) If Session("SortColumn") = e.SortExpression.ToString() Then 'Same sort expression used as last time so reverse the order If Session("SortOrder") = "" Then Session("SortOrder") = " DESC" Else Session("SortOrder") = "" End If End If Session("SortColumn") = e.SortExpression.ToString() Session("SortExpression") = e.SortExpression.ToString() + Session("SortOrder") gridLeaderBoard.DataSource = createDataSource() gridLeaderBoard.DataBind() End Sub Sub Grid_changed(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) gridLeaderBoard.CurrentPageIndex = e.NewPageIndex gridLeaderBoard.DataSource = createDataSource() gridLeaderBoard.DataBind() End Sub Function createDataSource() As ICollection Dim dt As New DataTable Dim dr As DataRow Dim cmdSelect As SqlCommand Dim dtrReader As SqlDataReader dt.Columns.Add(New DataColumn("Position", GetType(Short))) dt.Columns.Add(New DataColumn("UserName", GetType(String))) dt.Columns.Add(New DataColumn("played", GetType(Short))) dt.Columns.Add(New DataColumn("points", GetType(Short))) dt.Columns.Add(New DataColumn("Average", GetType(Single))) dt.Columns.Add(New DataColumn("highest", GetType(Short))) club256.connection.connect(Application("SQLServerAddress"), con256club) cmdSelect = New SqlCommand("up_sel_leaderboard2", con256club) cmdSelect.CommandType = CommandType.StoredProcedure dtrReader = cmdSelect.ExecuteReader Dim i As Short = 1 While dtrReader.Read dr = dt.NewRow dr(0) = i dr(1) = dtrReader("screen_name") dr(2) = dtrReader("played") dr(3) = dtrReader("totalscore") dr(4) = Decimal.Round(dtrReader("averagescore"), 3) dr(5) = dtrReader("highest") dt.Rows.Add(dr) i += 1 End While dtrReader.Close() cmdSelect.Dispose() club256.connection.Disconnect(Application("SQLServerAdress"), con256club) Dim dv As New DataView(dt) dv.Sort = Session("SortExpression") Return dv End Function
EDIT : I should just have combined this answer with you're other datagrid sort post really.
Thanks,
I had got the sortexpression working OK I hadn't found where to define Ascending or descending.
It really helped