Gridview not maintaining sort after paging
<p>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.</p>

<p>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.</p>

<p>What do I need to change in my Paging method to remember the sort?</p>

<p>Here is the code for the GridView:</p>

<pre><code>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 &amp; " " &amp; 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
</code></pre>

<p>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!</p>
