Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

GridView sorting

Posted on 2006-05-08
5
Medium Priority
?
996 Views
Last Modified: 2010-05-18

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?

0
Comment
Question by:oslonet
  • 2
4 Comments
 
LVL 10

Expert Comment

by:SystemExpert
ID: 16629665
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
0
 

Author Comment

by:oslonet
ID: 16629797
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; }
    }
0
 

Author Comment

by:oslonet
ID: 16629883
Found the solution. Needed to rebind in my sorting handler.
0
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16978051
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question