GridView sorting

Posted on 2006-05-08
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;
            GridViewSortDirection = SortDirection.Ascending;

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

BuildRanking sorts the dataview and binds it to the gridview.

Any help out there?

Question by:oslonet
    LVL 10

    Expert Comment

    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
          if(ViewState[SortOrder].ToString () == "ASC" || ViewState[SortOrder].ToString () =="")
                dvTable.Sort = sortExp + " ASC";
                ViewState[SortOrder] = "DESC" ; //store  it for Next Time
                dvTable.Sort = sortExp + " DESC";
                ViewState[SortOrder] = "ASC" ; // store it for Next Time

    Author Comment

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

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

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

    Author Comment

    Found the solution. Needed to rebind in my sorting handler.

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now