Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Paging and sorting

Paging and sorting

Scheduled Pinned Locked Moved ASP.NET
algorithmshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sandraa
    wrote on last edited by
    #1

    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 = True

            End If
    
        Catch ex As Exception
    
        End Try
    

    End Sub
    Private Property GridViewSortDirection() As String

        Get
    
            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
    
    A 1 Reply Last reply
    0
    • S Sandraa

      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 = True

              End If
      
          Catch ex As Exception
      
          End Try
      

      End Sub
      Private Property GridViewSortDirection() As String

          Get
      
              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
      
      A Offline
      A Offline
      Andreas X
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups