Hi Guys,
I have a grid that binds the data at runtime...
Dim myCommand = New SqlCommand(strSql, myConnection)
Dim dr As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
gvLiveCourses.Visible = True
gvLiveCourses.DataSourceID = ""
gvLiveCourses.DataSource = dr
gvLiveCourses.DataBind()
Next is the OnSort event and function. Actually I just copied it from other forums.
Sub gvLiveCourses_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
Dim m_DataTable As DataTable = TryCast(gvLiveCourses.DataSource, DataTable)
If m_DataTable IsNot Nothing Then
Dim m_DataView As New DataView(m_DataTable)
m_DataView.Sort = e.SortExpression + " " & ConvertSortDirectionToSql(e.SortDirection)
gvLiveCourses.DataSource = m_DataView
gvLiveCourses.DataBind()
End If
End Sub
Function ConvertSortDirectionToSql(ByVal sortDirection As SortDirection)
Dim newSortDirection As String = String.Empty
Select Case (sortDirection)
Case sortDirection.Ascending
newSortDirection = "ASC"
Case sortDirection.Descending
newSortDirection = "DESC"
End Select
Return newSortDirection
End Function
Now, when I click on the gridview columns it doesn't sort. And when I traced it using breakpoints, I found out that the m_DataTable is empty. But how come this becomes empty since the grid displays the data. I don't get it.
Please let me know on how to correct this code. I am using sqldatareader most of my applications, and I need to add sorting feature on every gridview. There's no problem in the gridview, only the sort feature that has to be added.
I hope you could give me a sample on sorting the gridview using sqldatareader that binds the data at runtime.
Thanks
Hifiger