我编写了我的程序代码,以便当用户点击某列时,数据按升序/降序排序。
然而,当我点击列标题时,实际上并没有发生任何事情。
我没有得到任何错误。
以下是我用来对GridView中的记录进行排序的代码:
Property GridViewSortDirection() As SortDirection
Get
If IsNothing(ViewState.Item("GridViewSortDirection")) Then
Return SortDirection.Descending
End If
Return ViewState.Item("GridViewSortDirection")
End Get
Set(ByVal Value As SortDirection)
ViewState.Item("GridViewSortDirection") = Value
End Set
End Property
Function GetSortDirection() As SortDirection
Dim GridViewSortDirectionNew As SortDirection
Select Case GridViewSortDirection
Case SortDirection.Descending
GridViewSortDirectionNew = SortDirection.Ascending
Case SortDirection.Ascending
GridViewSortDirectionNew = SortDirection.Descending
Case Else
GridViewSortDirectionNew = SortDirection.Descending
End Select
GridViewSortDirection = GridViewSortDirectionNew
Return GridViewSortDirectionNew
End Function
Protected Sub GridView_Sorting1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView2.Sorting
'Dim myPageIndex As Integer = GridView2.PageIndex
Dim mySortdirection As SortDirection = GetSortDirection()
'Dim sortExpression = e.SortExpression
'Dim dv As New DataView()
If (GridViewSortDirection = SortDirection.Ascending) Then
GridViewSortDirection = SortDirection.Descending
Else
GridViewSortDirection = SortDirection.Ascending
End If
End Sub
如果有人可以提供建议,那就太棒了。
非常感谢,
丹
答案 0 :(得分:0)
您必须使用Order By [COlName] ASC或DESC子句重新查询数据库。您将获得一个可以绑定到gridview的新数据集。
或者,您可以将初始结果放在数据视图中,然后在单击gridview列时对其进行相应的排序。这样,您不必再次访问数据库