Link to home
Start Free TrialLog in
Avatar of mgmhicks

asked on

How to sort gridview

I am using the following code to try and sort a gridview.  I have a column that has a sort expression of name (the field Name) and the grid is enabled for paging and sorting.  When I run the following code it ends up in a loop and I cant figure out why.  It then crashes.  Where have I missed the boat.

Protected Sub SelectionGrid_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
        If e.SortDirection = SortDirection.Ascending Then
            SelectionGrid.Sort(e.SortExpression, SortDirection.Descending)
            SelectionGrid.Sort(e.SortExpression, SortDirection.Ascending)
        End If
    End Sub

Open in new window

Avatar of Sekurity863
Flag of United States of America image

Your code is looping because you have a sort statement embedded in a if sort statement.

everytime you set the SelectringGrid.Sort, it calls the Subroutine. So everytime it sorts, it evaluates how to sort it basically running the sort statement over and over.
Avatar of mgmhicks


I see that know.  Then how do I change the sort direction when the gridview column header is clicked.
This should be handled without code.  Just set your field for sorting then thats it.  No code necessary.
Under your Gridview columns make sure Sorting is set to automatic
If I do nothing put set the sort expression which I have done when I created the columns, nothing happens when I click the column.  The column clicks but the order is never changed.
Do it in the datagrid directly (no code.)  For example:  SortExpression="FirstName"

Make sure your (1) query has the field (2) the grid has the field and (3) the sort expression is EXACTLY correct.
Do you have  AllowPaging="True" AllowSorting="True"
The grid fills fine with the dataset, the query pulls all fields, the grid has the names of the bound fields and they bind ok, and the sort expression as the name of the field.  I tried sort expression "Name" with quotes that didnt help.  
Here is the gridview code:

<asp:GridView ID="SelectionGrid" runat="server" AutoGenerateColumns="False"
                Height="80px" Width="788px" CellPadding="2" ForeColor="Black"
                GridLines="None" PageSize="5" RowHeaderColumn="CallID"
                BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
                AllowSorting="True" OnSorting="SelectionGrid_Sorting" AllowPaging="True"
                OnSorted="SelectionGrid_Sorted" OnPageIndexChanging="SelectionGrid_PageIndexChanging">
                <FooterStyle BackColor="Tan" />
                    <asp:BoundField HeaderText="Call ID" DataField="CallID"
                        SortExpression="CallID" />
                    <asp:BoundField HeaderText="Site Name" DataField="Name"
                        SortExpression="&quot;Name&quot;" />
                    <asp:BoundField HeaderText="Request Date/Time" DataField="CreatedDate" />
                    <asp:BoundField HeaderText="Priority" DataField="Priority"
                        SortExpression="Priority" />
                    <asp:BoundField HeaderText="Category" DataField="Category" />
                    <asp:ButtonField Text="Edit" />
                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                    HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                <HeaderStyle BackColor="Tan" Font-Bold="True" />
                <AlternatingRowStyle BackColor="PaleGoldenrod" />
I'm thinking that "Name" might be a reserved word.  Also your SortExpression is wierd - "&quot;Name&quot;".  Trying to change it to "MyName" in both the query and the sort expression.

Also you can try just dragging/dropping the gridview onto a new test page and let the wizard walk u through everything.  Does that work?
This was created when I test putting quotes in.  That has since been removed.  I thought about the name thing, but I have other sort columns and they are doing the same thing,

I will try the new grid idea.  I know I'm missing something simple somewhere.
Yes, go back to the basics.  Create a new aspx page and just drag/drop the gridview on the form.  Go thru the wizard and allow sorting, etc...  Does it work?  :)
Also, I would not use "Name" as a field name.  That might cause issues places...  

In the above experiment let me know if ANY of the sorting works.  
Hi RobertNZana,
I did as you suggested put a gridview on a blank web page,  Binded the grid and still got the same results.  At first it gave me a selectgrid.sorting sub not handled.  Then I added the sub, but no code and it runs without error but sorting doesnt change.  There has to be some kind of code that goes into the selectiongrid_sorting and selectiongrid_pagechanging events to make this work.  Just binding and telling it that it should sort and page doesnt work.


Added the following code to generic test page and it still doesnt work.

Protected Sub SelectionGrid_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles SelectionGrid.Sorting
        Dim strSort As String = e.SortExpression.ToString
        If strSort = "CallID" Or strSort = "Category" Then
            If Not Boolean.Parse(Session("firstSort")) Then
                If e.SortDirection = SortDirection.Ascending Then
                    e.SortDirection = SortDirection.Ascending
                    e.SortDirection = SortDirection.Descending
                End If
                e.SortDirection = SortDirection.Descending
                Session("firstSort") = False
            End If
            ' This disables the other columns sort (you can remove)
            e.Cancel = True
        End If
           End Sub

Open in new window

Avatar of gadavis2

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial