Gridview在分页后不保持排序

时间:2012-02-19 16:55:49

标签: asp.net

我从类似帖子中找到的有关此问题的所有帮助都不适用于我如何设置GridView。

我已经让我的Gridview动态创建了列,并且能够对其进行排序。我的分页也有效,但是如果我先排序然后转到另一个页面,它就会失去排序。

在Paging方法中需要更改哪些内容才能记住排序?

以下是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

排序和分页工作,我只是不确定我必须在分页方法中更改以记住排序。谢谢你的帮助!

2 个答案:

答案 0 :(得分:3)

这是一个完整的示例代码,包括分页,排序和数据绑定:

Sub Page_load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        CreateGridColumns()
        BindGrid()
    End If
End Sub

Public Property SortExpression As String
    Get
        If ViewState("SortExpression") Is Nothing Then
            ViewState("SortExpression") = "LastName ASC"
        End If
        Return ViewState("SortExpression").ToString
    End Get
    Set(value As String)
        ViewState("SortExpression") = value
    End Set
End Property

Private Sub CreateGridColumns()
    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)
End Sub

Private Sub BindGrid()
    Try
        Dim tblData = New DataTable
        Using sqlCon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString())
            Dim sql As String = "SELECT * FROM EmployeeList ORDER BY {0}"
            Dim sqlCmd = New SqlClient.SqlCommand()
            sqlCmd.CommandText = String.Format(sql, Me.SortExpression)
            sqlCmd.Connection = sqlCon
            Using objAdapter As New SqlClient.SqlDataAdapter(sqlCmd)
                objAdapter.Fill(tblData)
            End Using
        End Using
        GridView1.DataSource = tblData 
        GridView1.DataBind()
    Catch ex As Exception
        ' TODO: log error '
        throw
    End Try
End Sub

Private Sub GridView1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    Me.GridView1.PageIndex = e.NewPageIndex
    BindGrid()
End Sub

Private Sub GridView1_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
    Dim currentSortColumn, currentSortDirection As String
    currentSortColumn = Me.SortExpression.Split(" "c)(0)
    currentSortDirection = Me.SortExpression.Split(" "c)(1)
    If e.SortExpression.Equals(currentSortColumn) Then
        ' switch sort direction '
        Select Case currentSortDirection.ToUpper
            Case "ASC"
                Me.SortExpression = currentSortColumn & " DESC"
            Case "DESC"
                Me.SortExpression = currentSortColumn & " ASC"
        End Select
    Else
        Me.SortExpression = e.SortExpression & " ASC"
    End If
    BindGrid()
End Sub

答案 1 :(得分:0)

在“业务”决定带有gridview的Web窗体应用程序需要特定的“事件等等......”所有事件,回发,重定向等......我得出了解放的结论。< / p>

  1. 我没有为一些流量大的公司维护这个应用程序(因此我使用数据库的解决方案)
  2. 会话不适合超时并使用2个节点进行服务器循环
  3. 隐藏字段将在各种操作中丢失
  4. ViewState在各种操作中迷失
  5. Cookie不适用于使用不同计算机的用户
  6. Querystring - 丑陋且有些问题。
  7. 我的解决方案是,天才就是跳过所有这些并将重要数据字符串保存到json中的数据库表中。

    我意识到这不是每个人都想做的事情,但在各种情况下都像我一样。没有大量的逻辑检查和设置ViewState等,这是令人耳目一新的。