Gridview sorting: SortDirection always Ascending

Posted by Julien N on Stack Overflow See other posts from Stack Overflow or by Julien N
Published on 2008-10-30T12:41:01Z Indexed on 2010/05/05 13:08 UTC
Read the original article Hit count: 623

Filed under:
|

Hi !

I have a gridview and I need to sort its elements when the user clicks on the header.
Its datasource is a List object.

The aspx is defined this way :

            <asp:GridView ID="grdHeader" AllowSorting="true" AllowPaging="false" 
                AutoGenerateColumns="false" Width="780" runat="server"  OnSorting="grdHeader_OnSorting" EnableViewState="true">
                <Columns>
                    <asp:BoundField DataField="Entitycode" HeaderText="Entity" SortExpression="Entitycode" />
                    <asp:BoundField DataField="Statusname" HeaderText="Status" SortExpression="Statusname" />
                    <asp:BoundField DataField="Username" HeaderText="User" SortExpression="Username" />
                </Columns>
            </asp:GridView>

The code behind is defined this way :
First load :

protected void btnSearch_Click(object sender, EventArgs e)
{
    List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
    this.grdHeader.DataSource = items;
    this.grdHeader.DataBind();
}

when the user clicks on headers :

protected void grdHeader_OnSorting(object sender, GridViewSortEventArgs e)
{
    List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
    items.Sort(new Helpers.GenericComparer<V_ReportPeriodStatusEntity>(e.SortExpression, e.SortDirection));
    grdHeader.DataSource = items;
    grdHeader.DataBind();
}

My problem is that e.SortDirection is always set to Ascending.
I have webpage with a similar code and it works well, e.SortDirection alternates between Ascending and Descending.

What did I do wrong ?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about gridview