gpt4 book ai didi

asp.net - Gridview分页后不保持排序

转载 作者:行者123 更新时间:2023-12-02 15:12:11 24 4
gpt4 key购买 nike

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

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

我需要在分页方法中更改什么才能记住排序?

这是 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

排序和分页工作正常,我只是不确定我的分页方法需要更改什么才能记住排序。感谢您的帮助!

最佳答案

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

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

关于asp.net - Gridview分页后不保持排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9351121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com