GridView sorting: SortDirection always Ascending GridView sorting: SortDirection always Ascending asp.net asp.net

GridView sorting: SortDirection always Ascending


The problem with Session and Viewstate is that you also have to keep track of the gridview control for which SortColumn and Direction is stored if there is more than one gridview on the page.

An alternative to Session and Viewstate is to add 2 attributes to the Gridview and keep track of Column and Direction that way.

Here is an example:

private void GridViewSortDirection(GridView g, GridViewSortEventArgs e, out SortDirection d, out string f){    f = e.SortExpression;    d = e.SortDirection;    //Check if GridView control has required Attributes    if (g.Attributes["CurrentSortField"] != null && g.Attributes["CurrentSortDir"] != null)    {        if (f == g.Attributes["CurrentSortField"])        {            d = SortDirection.Descending;            if (g.Attributes["CurrentSortDir"] == "ASC")            {                d = SortDirection.Ascending;            }        }        g.Attributes["CurrentSortField"] = f;        g.Attributes["CurrentSortDir"] = (d == SortDirection.Ascending ? "DESC" : "ASC");    }}


You can use a session variable to store the latest Sort Expression and when you sort the grid next time compare the sort expression of the grid with the Session variable which stores last sort expression. If the columns are equal then check the direction of the previous sort and sort in the opposite direction.

Example:

DataTable sourceTable = GridAttendence.DataSource as DataTable;DataView view = new DataView(sourceTable);string[] sortData = ViewState["sortExpression"].ToString().Trim().Split(' ');if (e.SortExpression == sortData[0]){    if (sortData[1] == "ASC")    {        view.Sort = e.SortExpression + " " + "DESC";        this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";    }    else    {        view.Sort = e.SortExpression + " " + "ASC";        this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";    }}else{    view.Sort = e.SortExpression + " " + "ASC";    this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";}


A simple solution:

protected SortDirection GetSortDirection(string column){    SortDirection nextDir = SortDirection.Ascending; // Default next sort expression behaviour.    if (ViewState["sort"] != null && ViewState["sort"].ToString() == column)    {   // Exists... DESC.        nextDir = SortDirection.Descending;        ViewState["sort"] = null;    }    else    {   // Doesn't exists, set ViewState.        ViewState["sort"] = column;    }    return nextDir;}

Much like the default GridView sorting and lightweight on the ViewState.

USAGE:

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