Avatar of mgmhicks
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)
        Else
            SelectionGrid.Sort(e.SortExpression, SortDirection.Ascending)
        End If
    End Sub

Open in new window

ASP.NETVisual Basic Classic.NET Programming

Avatar of undefined
Last Comment
gadavis2

8/22/2022 - Mon
Sekurity863

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.
ASKER
mgmhicks

I see that know.  Then how do I change the sort direction when the gridview column header is clicked.
RobertNZana

This should be handled without code.  Just set your field for sorting then thats it.  No code necessary.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Sekurity863

Under your Gridview columns make sure Sorting is set to automatic
ASKER
mgmhicks

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.
RobertNZana

Do it in the datagrid directly (no code.)  For example:  SortExpression="FirstName"

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
RobertNZana

Make sure your (1) query has the field (2) the grid has the field and (3) the sort expression is EXACTLY correct.
Sekurity863

Do you have  AllowPaging="True" AllowSorting="True"
ASKER
mgmhicks

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.  
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
mgmhicks

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" />
                <Columns>
                    <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" />
                </Columns>
                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                    HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                <HeaderStyle BackColor="Tan" Font-Bold="True" />
                <AlternatingRowStyle BackColor="PaleGoldenrod" />
            </asp:GridView>
RobertNZana

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?
ASKER
mgmhicks

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
RobertNZana

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?  :)
RobertNZana

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.  
ASKER
mgmhicks

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.

Thanks

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
mgmhicks

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
                Else
                    e.SortDirection = SortDirection.Descending
                End If
            Else
                e.SortDirection = SortDirection.Descending
                Session("firstSort") = False
            End If
        Else
            ' This disables the other columns sort (you can remove)
            e.Cancel = True
        End If
           End Sub

Open in new window

ASKER CERTIFIED SOLUTION
gadavis2

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question