vb.net 未处理的 Gridview 排序事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18451702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gridview Sorting event unhandled
提问by Oumie
I have an asp gridviewwhich is connected to my sqldatabase via LINQ. I have it bound in the code behind. I also do the usual,
我有一个aspgridview连接到我sql通过数据库LINQ。我把它绑定在后面的代码中。我也照常,
AllowSorting="True"
and I set the sort expression for each column: ex-
我为每一列设置了排序表达式:ex-
<asp:BoundField DataField="BorrowerDateOfBirth" HeaderText="Date Of Birth"
DataFormatString="{0:d}" SortExpression="BorrowerDateOfBirth" >
</asp:BoundField>
But when I run the application, and click the column headers to sort, the application fires an exception error that reads:
但是当我运行应用程序并单击列标题进行排序时,应用程序会触发一个异常错误,内容如下:
"The GridView 'gridview1' fired event Sorting which wasn't handled."
“GridView ' gridview1' 触发了未处理的事件排序。”
I looked this error up online but I only found responses related to C# code. I tried converting them to vb.net but the error still persisted.
我在网上查找了这个错误,但我只找到了与 C# 代码相关的响应。我尝试将它们转换为 vb.net,但错误仍然存在。
Does anyone know how to handle sorting of an asp gridview in vb.net?
有谁知道如何在 vb.net 中处理 asp gridview 的排序?
采纳答案by Derek Meyer
you need to set the OnSorting=""property to some function name, and then handle the sorting in said function. something along these lines
您需要将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

