0

I have a gridview with sorting enabled:

<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" AllowSorting="true" OnSorting="grid1_Sorting">
    <Columns>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <ItemTemplate>
                <asp:Label ID="NameLabel" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

Then in my sorting event I have

VB.NET

Page load

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ViewState("Order") = "DESC"
    End If
End Sub

    Protected Sub grid1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles grid1.Sorting
 Select Case e.SortExpression
            Case "Name"
                If ViewState("Order") = "DESC" Then
                    grid1.DataSource = DescData()
                    grid1.DataBind()
                    ViewState("Order") = "ASC"
                    Exit Select
                Else
                    grid1.DataSource = AscData()
                    grid1.DataBind()
                    ViewState("SortOrder") = "DESC"
                    Exit Select
                End If
End Select

C#

protected void grid1_Sorting(object sender, GridViewSortEventArgs e)
{
    switch (e.SortExpression) {
        case "Name":
            if (e.SortDirection == SortDirection.Ascending) {
                grid1.DataSource = AscendingData();
                grid1.DataBind();
                e.SortDirection = SortDirection.Descending;
                break; 
            } else {
                grid1.DataSource = DescendingData();
                grid1.DataBind();
                e.SortDirection = SortDirection.Ascending;
                break; 
            }
            break;
    }
}

I've read many articles but most seem to target a dataTable or using Viewstate. This gives me the impression I am missing something to get the above working.

The problem I have is when I click a column to sort it goes through BOTH if conditions (that is Ascending and then Descending code) but I don't understand why?

Computer
  • 2,149
  • 7
  • 34
  • 71
  • known issue http://stackoverflow.com/questions/250037/gridview-sorting-sortdirection-always-ascending – Sachin Feb 08 '14 at 10:56
  • I've read that but as i mentioned in my post it targets datatable or using Viewstate. Are you suggesting that's the correct way to achieve this (using Viewstate in this case)? – Computer Feb 08 '14 at 11:06
  • Yes you can use the viewstate to overcome this problem.. – Sachin Feb 08 '14 at 11:07
  • I've updated my code but still have the same issue. Am i missing something? – Computer Feb 08 '14 at 11:20

1 Answers1

0

Ok the issue was in the aspx page. The gridview has OnSorting="grid1_Sorting". Removing this line resolved it.

I think i must have replicated some C# code and added that line of code. Removing that line resolved the issue as it was causing the GV to sort twice.

Computer
  • 2,149
  • 7
  • 34
  • 71