2013-08-26 69 views
1

我有一個asp gridview它通過LINQ連接到我的sql數據庫。我將它綁定在後面的代碼中。我也做平常,Gridview排序事件未處理

AllowSorting="True" 

,我爲每個列的排序表達式:EX-

   <asp:BoundField DataField="BorrowerDateOfBirth" HeaderText="Date Of Birth" 
        DataFormatString="{0:d}" SortExpression="BorrowerDateOfBirth" > 
       </asp:BoundField> 

但是當我運行應用程序,並單擊列標題,應用火災排序一個異常錯誤,內容如下:

「The GridView'gridview1'觸發了未處理的事件排序。」

我在網上看到這個錯誤,但我只找到與C#代碼相關的響應。我試圖將它們轉換爲vb.net,但錯誤仍然存​​在。

有誰知道如何處理vb.net中的asp gridview的排序?

回答

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 
+0

在這種情況下,OnSorting =「」的函數名稱是在你的例子? – Oumie

+0

TaskGridView_Sorting –