Member Avatar for Geek-Master

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?

Thanks,
Daniel

Actually there is a bug in gridview, it always returns as SortDirection.Ascending , so you have to keep the sort direction in viewstate and manually switch it each time the page is post back.

It must be something like this:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        DataTable dt =  GetData();
         DataView dv = dt.DefaultView;
        if (ViewState["_Price"] == null)
        {
            ViewState["_Price"] = "DESC";

        }
        else
        {
            if (ViewState["_Price"].ToString() == "ASC")
            {
                ViewState["_Price"] = "DESC";
            }
            else
            {
                ViewState["_Price"] = "ASC";
            }
        }
        string __Price = ViewState["_Price"].ToString();
        dv.Sort = e.SortExpression + " " + __Price;

        GridView1.DataSource = dv;
        GridView1.DataBind();
    }
Member Avatar for Geek-Master

I've ran into that GridView bug with sorting data. My fix was to create a global variable boolean data type for the page and then just toggle it whenever the event was shot off. Just like you said whenever post back occurs.

However, the sorting bug is a problem, my biggest concern right now is getting the datasource from the gridview into a workable table like in the example I included in my first post.

If you are worried about sorting like this (and I don't know the complexity of your gridview), you can always put it into a repeater. That way your entire sorting is done through the SQL Server and you have 100% control.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.