1

我有一个gridview通过. 我将它绑定在后面的代码中。我也照常做,sqlLINQ

 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 的排序?

4

1 回答 1

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
于 2013-08-26T19:42:33.773 回答