我有一个asp gridview
,它通过sql
连接到我的LINQ
数据库。我把它绑在后面的代码中。我也照常做,
AllowSorting="True"
我为每列设置了排序表达式:ex-
<asp:BoundField DataField="BorrowerDateOfBirth" HeaderText="Date Of Birth"
DataFormatString="{0:d}" SortExpression="BorrowerDateOfBirth" >
</asp:BoundField>
但是当我运行应用程序并单击列标题进行排序时,应用程序会触发一个异常错误,内容如下:
“GridView'gridview1
'触发事件排序未处理。”
我在线查看了这个错误,但我只找到了与C#代码相关的回复。我尝试将它们转换为vb.net,但错误仍然存在。
有谁知道如何在vb.net中处理asp gridview的排序?
答案 0 :(得分:1)
您需要将OnSorting=""
属性设置为某个函数名,然后在所述函数中处理排序。沿着这些方向的东西
Protected Sub TaskGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
'Retrieve the table from the session object.
Dim dt = TryCast(Session("table"), DataTable)
If dt IsNot Nothing Then
'Sorting the data.
dt.DefaultView.Sort = e.SortExpression & " " & GetSortingDirection(e.SortExpression)
TaskGridView.DataSource = Session("TaskTable")
TaskGridView.DataBind()
End If
End Sub
Private Function GetSortingDirection(ByVal column As String) As String
' By default, set the sort direction to ascending.
Dim sortDirection = "ASC"
' Retrieve the last column that was sorted.
Dim sortExpression = TryCast(ViewState("SortExpression"), String)
If sortExpression IsNot Nothing Then
' Check if the same column is being sorted.
' Otherwise, the default value can be returned.
If sortExpression = column Then
Dim lastDirection = TryCast(ViewState("SortDirection"), String)
If lastDirection IsNot Nothing _
AndAlso lastDirection = "ASC" Then
sortDirection = "DESC"
End If
End If
End If
' Save new values in ViewState.
ViewState("SortDirection") = sortDirection
ViewState("SortExpression") = column
Return sortDirection
End Function