Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:24:54+00:00 2026-06-12T04:24:54+00:00

I’m currently trying write code that will maintain the sorting preference while changing page

  • 0

I’m currently trying write code that will maintain the sorting preference while changing page on a GridView. My current code is working fine to sort the data based on the user sorting selection. But when changing page, the sorting doesn’t work. I don’t get an error but rather unsorted results.

Here’s my aspx code:

<asp:GridView ID="tblAdministrators" runat="server" AutoGenerateColumns="false" EmptyDataText="No records found" PageSize="75" AllowPaging="True" AllowSorting="True">

  <Columns>
    <asp:BoundField HeaderText="Name" InsertVisible="False" DataField="FULLNAME" SortExpression="FULLNAME"></asp:BoundField>
  </Columns>
  <Columns>
   <asp:BoundField HeaderText="Active" InsertVisible="False" DataField="ACTIVE" SortExpression="ACTIVE"></asp:BoundField>
  </Columns>

</asp:GridView>

Here’s my VB code:

Public Class ViewUsers
Inherits SolutionBasePage

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
        tblAdministrators.DataSource = GetData()
        tblAdministrators.DataBind()
    End If

End Sub

Private Const ASCENDING As String = " ASC"
Private Const DESCENDING As String = " DESC"

Private Function GetData() As Data.DataView

    Dim connection As OracleDBConnect = DAL.GetOracleDBConnection()
    Dim request As OracleDBRequest = Nothing
    Dim result As OracleDBResult = Nothing
    Dim trace As OracleDBChronoTrace = Nothing
    Dim status As DBStatus
    Dim sb As New StringBuilder
    Dim dv As DataView
    Dim ds As Data.DataSet = New Data.DataSet

    Try

        With sb
            .Append("SELECT FULLNAME, ACTIVE FROM USERS")
        End With

        request = New OracleDBRequest(sb.ToString, CommandType.Text)

        status = connection.Execute(request, result, trace)

        dv = New DataView(result.DataSet.Tables(0))

        If (ViewState("sortExp") IsNot Nothing) Then
            dv = New Data.DataView(result.DataSet.Tables(0))

            If (GridViewSortDirection = SortDirection.Ascending) Then
                GridViewSortDirection = SortDirection.Descending
                dv.Sort = CType(ViewState("sortExp") & DESCENDING, String)
            Else
                GridViewSortDirection = SortDirection.Ascending
                dv.Sort = CType(ViewState("sortExp") & ASCENDING, String)
            End If
        Else
            dv = result.DataSet.Tables(0).DefaultView
        End If

        Return dv

    Catch ex As Exception

    Finally
        'ds.Dispose()
        'dp.Dispose()
    End Try

End Function

Public Property GridViewSortDirection() As SortDirection
    Get
        If ViewState("sortDir") Is Nothing Then
            ViewState("sortDir") = SortDirection.Ascending
        End If

        Return CType(ViewState("sortDir"), SortDirection)
    End Get

    Set(ByVal value As SortDirection)
        ViewState("sortDir") = value
    End Set

End Property


Protected Sub tblAdministrators_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles tblAdministrators.PageIndexChanging

    tblAdministrators.PageIndex = e.NewPageIndex
    tblAdministrators.DataSource = GetData()
    tblAdministrators.DataBind()

End Sub


Protected Sub tblAdministrators_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles tblAdministrators.Sorting

    ViewState("sortExp") = e.SortExpression
    tblAdministrators.DataSource = GetData()
    tblAdministrators.DataBind()

End Sub

End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:24:55+00:00Added an answer on June 12, 2026 at 4:24 am

    The problem is whenever you call the GetData() function, your code is changing the dataview’s sort property even if it’s being called by PageIndexChaging event.

    If (GridViewSortDirection = SortDirection.Ascending) Then
        GridViewSortDirection = SortDirection.Descending
        dv.Sort = CType(ViewState("sortExp") & DESCENDING, String)
    Else
        GridViewSortDirection = SortDirection.Ascending
        dv.Sort = CType(ViewState("sortExp") & ASCENDING, String)
    End If
    

    You can modify your GetData() function to accept a SortDirection. Then, in your tblAdministrators_PageIndexChanging and tblAdministrators_Sorting events, just pass the GridViewSortDirection. Something like this:

    Private Function GetData(sort As SortDirection) As Data.DataView
    ' your code here...
    End Function
    
    Protected Sub tblAdministrators_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles tblAdministrators.PageIndexChanging
        tblAdministrators.PageIndex = e.NewPageIndex
        tblAdministrators.DataSource = GetData(GridViewSortDirection)
        tblAdministrators.DataBind()
    End Sub
    
    Protected Sub tblAdministrators_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles tblAdministrators.Sorting
        ViewState("sortExp") = e.SortExpression
        GridViewSortDirection = If(GridViewSortDirection = SortDirection.Descending, SortDirection.Ascending, SortDirection.Descending)
        tblAdministrators.DataSource = GetData(GridViewSortDirection)
        tblAdministrators.DataBind()
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.