Link to home
Start Free TrialLog in
Avatar of oslonet
oslonet

asked on

GridView sorting


I create a GridView programatically and bind it to a DataView. Works fine until it comes to sorting. Clicking those columns that i have set to be sortable makes
my event handler fire only every second time. Sorting becomes correct, but it always needs two clicks to be correct.

I create my GridView like this:

GridView gw = new GridView();
gw.AutoGenerateColumns = false;
gw.GridLines = GridLines.Horizontal;
gw.AllowSorting = true;
gw.Sorting += new GridViewSortEventHandler(Sorting);


The event handler:

    public void Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortExpression"] = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
            GridViewSortDirection = SortDirection.Descending;
        else
            GridViewSortDirection = SortDirection.Ascending;

        BuildRanking(ViewState["sortExpression"].ToString(), ViewState["sortDirection"].ToString());
    }

BuildRanking sorts the dataview and binds it to the gridview.

Any help out there?

Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Hi ,

You require to save the Order of the column

that is ASC or DESC
==========================
dvTable.Sort = sortExp + " ASC";
                        
if (ViewState[SortOrder]==null)
{
      //If no any previous order then set default ASC
      ViewState[SortOrder]="ASC";
}
      if(ViewState[SortOrder].ToString () == "ASC" || ViewState[SortOrder].ToString () =="")
      {      
            dvTable.Sort = sortExp + " ASC";
            ViewState[SortOrder] = "DESC" ; //store  it for Next Time
      }
                else
      {
            dvTable.Sort = sortExp + " DESC";
            ViewState[SortOrder] = "ASC" ; // store it for Next Time
      }
                        
      dgApp.DataSource=dvTable;
      dgApp.DataBind();
==========================
Thanks
Avatar of oslonet
oslonet

ASKER

It is saved... Didn't post that part of the code....

    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Descending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
Avatar of oslonet

ASKER

Found the solution. Needed to rebind in my sorting handler.
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

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