I've searched every where for any code examples for a manual sorting routine, but the only ones I can find are in C#. I've been able to convert it into its VB counterpart, however I'm having trouble with the data casting.
The original C# code came from this address http://forums.asp.net/t/956540.aspx
Here is what I came up with in VB
Private Function getSortDirectionSQL(ByVal sortDir As SortDirection) As String If sortDir = SortDirection.Ascending Then Return "ASC" ElseIf sortDir = SortDirection.Descending Then Return "DESC" Else ' Default to Ascending Return "ASC" End If End Function Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As _ System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting Dim DataTable1 As DataTable = TryCast(GridView1.DataSource, DataTable) If Not DataTable1 Is Nothing Then Dim DataView1 As DataView = New DataView(DataTable1) DataView1.Sort = e.SortExpression + " " + getSortDirectionSQL(e.SortDirection) Me.GridView1.DataSource = DataView1 Me.GridView1.DataBind() End If End Sub
I assume that the C# code works, but when I cast the GridView1.Datasource into a DataTable I get an error. I've tried a few other things, but no luck.
The reason why I want to manually sort the data and not use the built in sorting is because I'm not using the SqlDataSource control to bind to the GridView control. I have a search function that determines if the input was numerical (ID number) or a string of characters (Last Name or First Name combination). Then it picks the appropriate stored procedure to query the database with. So I'm forced to use the manual sorting.
Anyone have an idea?