Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview not maintaining sort after paging

Status
Not open for further replies.

Cineno

Programmer
Jul 24, 2006
142
US
All the help I've been able to find from similar posts regarding this issue isn't working for how I set up my GridView.

I've gotten my Gridview to have dynamically created columns, and was able to get sorting to work on it. My paging works also, but if I sort first and then go to another page, it loses the sort.

What do I need to change in my Paging method to remember the sort?

Here is the code for the GridView:

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


    If Not IsPostBack Then

        Dim curLastName As New BoundField
        curLastName.HeaderText = "Last Name"
        curLastName.DataField = "LastName"
        curLastName.SortExpression = "LastName"
        GridView1.Columns.Insert(0, curLastName)

        Dim curFirstName As New BoundField
        curFirstName.HeaderText = "First Name"
        curFirstName.DataField = "FirstName"
        curFirstName.SortExpression = "FirstName"
        GridView1.Columns.Insert(1, curFirstName)

        Dim dt As DataTable = GetData().Tables(0)

        Dim dv As New DataView(dt)



        GridView1.DataSource = dv

        GridView1.DataBind()
    End If
End Sub

Private Function GetData() As DataSet

    Dim connectionstr As String

    connectionstr = ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString()

    Dim myConnection As New SqlConnection(connectionstr)

    Dim ad As New SqlDataAdapter("SELECT * FROM EmployeeList where lastname like 'wil%'", myConnection)

    Dim ds As New DataSet()

    ad.Fill(ds)

    Return ds

End Function

Public Property GridViewSortDirection() As SortDirection


    Get


        If ViewState("sortDirection") Is Nothing Then

            ViewState("sortDirection") = SortDirection.Ascending
        End If


        Return DirectCast(ViewState("sortDirection"), SortDirection)
    End Get

    Set(ByVal value As SortDirection)
        ViewState("sortDirection") = value
    End Set
End Property


Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)


    Dim sortExpression As String = e.SortExpression

    If GridViewSortDirection = SortDirection.Ascending Then


        GridViewSortDirection = SortDirection.Descending


        SortGridView(sortExpression, "DESC")
    Else



        GridViewSortDirection = SortDirection.Ascending


        SortGridView(sortExpression, "ASC")
    End If

End Sub

Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)

    Dim dt As DataTable = GetData().Tables(0)

    Dim dv As New DataView(dt)

    dv.Sort = sortExpression & " " & direction

    GridView1.DataSource = dv

    GridView1.DataBind()

End Sub



Protected Sub GridView1_PageIndexChanging(ByVal sender As [Object], ByVal e As GridViewPageEventArgs)


    GridView1.PageIndex = e.NewPageIndex


    Dim dt As DataTable = GetData().Tables(0)

    Dim dv As New DataView(dt)



    GridView1.DataSource = dv

    GridView1.DataBind()



End Sub

The sorting and paging work, I'm just not sure what I have to change in my paging method to remember the sort. Thanks for any help!


 
As you're fetching the data every time the sort is lost. Either capture the fact that a sort has been applied, what it is and re-apply it when changing pages, or cache the data behind the grid and update the cached data with it's sorted equivalent when a sort is applied. That when when the page is changed you don't need to re-fetch the data from source, you need to r5e-fetch it from the cache.

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Thank you both. I got it working now. I appreciate the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top