I am trying to sort a gridview in code. It was working, and now I am getting a
Object reference not set to an instance of an object
error. It is happening on the line where I assign the gridview's datasource to a datatable. Here is the section of code (note, the gridivew works and pages just fine)
Protected Sub gvBasicList_Sorting(sender As Object, e As GridViewSortEventArgs)
Dim gv As GridView = DirectCast(sender, GridView)
Dim dv As DataView = TryCast(gv.DataSource, DataView) <ERROR HERE
Dim dataTable As DataTable = dv.Table
Dim sortdir As [String] = ""
If e.SortExpression <> "" And e.SortExpression IsNot Nothing Then
If gvSortExpression = e.SortExpression Then
gvSortDirection = GetSortDirection()
Else
gvSortDirection = "ASC"
End If
gvSortExpression = e.SortExpression
gvBasicList.EditIndex = -1
End If
Session("listsort") = e.SortExpression
Session("listsortdirection") = gvSortDirection
gv.DataSource = GetSortedData(dataTable, e.SortExpression, gvSortDirection)
gv.DataBind()
End Sub