Paging and sorting
-
Hi all, I have a little problem :) I have a gridview with paging and sorting, all work well except when I change the page then the gridview lost the sorting and it put again by default sorting. Any idea how maintein the sorting when I change of page? I send you my code, thanks for all.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not Page.IsPostBack Then
EnlazarDataGrid()
gridView.Columns(0).SortExpression = TrueEnd If Catch ex As Exception End Try
End Sub
Private Property GridViewSortDirection() As StringGet Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection")) End Get Set(ByVal value As String) ViewState("SortDirection") = value End Set End Property Private Property GridViewSortExpression() As String Get Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression")) End Get Set(ByVal value As String) ViewState("SortExpression") = value End Set End Property Private Function GetSortDirection() As String Select Case GridViewSortDirection Case "ASC" GridViewSortDirection = "DESC" Case "DESC" GridViewSortDirection = "ASC" End Select Return GridViewSortDirection End Function Protected Function SortDataTable(ByVal pdataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView If Not pdataTable Is Nothing Then Dim pdataView As New DataView(pdataTable) If GridViewSortExpression <> String.Empty Then If isPageIndexChanging Then pdataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection) Else pdataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection()) End If End If Return pdataView Else Return New DataView() End If End Function Protected Sub gridView\_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Try gridView.Columns(0).Visible = True Dim Comunidad As Integer
-
Hi all, I have a little problem :) I have a gridview with paging and sorting, all work well except when I change the page then the gridview lost the sorting and it put again by default sorting. Any idea how maintein the sorting when I change of page? I send you my code, thanks for all.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not Page.IsPostBack Then
EnlazarDataGrid()
gridView.Columns(0).SortExpression = TrueEnd If Catch ex As Exception End Try
End Sub
Private Property GridViewSortDirection() As StringGet Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection")) End Get Set(ByVal value As String) ViewState("SortDirection") = value End Set End Property Private Property GridViewSortExpression() As String Get Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression")) End Get Set(ByVal value As String) ViewState("SortExpression") = value End Set End Property Private Function GetSortDirection() As String Select Case GridViewSortDirection Case "ASC" GridViewSortDirection = "DESC" Case "DESC" GridViewSortDirection = "ASC" End Select Return GridViewSortDirection End Function Protected Function SortDataTable(ByVal pdataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView If Not pdataTable Is Nothing Then Dim pdataView As New DataView(pdataTable) If GridViewSortExpression <> String.Empty Then If isPageIndexChanging Then pdataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection) Else pdataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection()) End If End If Return pdataView Else Return New DataView() End If End Function Protected Sub gridView\_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Try gridView.Columns(0).Visible = True Dim Comunidad As Integer
Hello! I have seen this problem before and i have not found any better solution than to save the sortexpression value in a session variable. Every time the sortexpression is not null, save it to the session, and use the session when the sortexpression is null. somewhere i saw that this is a bug in the gridview that is exposed when you are databinding from codebehind, but it could be a rumor.
Andreas Johansson
IT Professional at Office IT Partner i Norrbotten Sweden
What we don't know. We learn.
What you don't know. We teach