GridView e.SortDirection always ascending

Last post 10-26-2009 12:30 PM by Beej. 16 replies.

Sort Posts:

  • GridView e.SortDirection always ascending

    02-11-2005, 10:03 AM
    • Member
      187 point Member
    • chadgei
    • Member since 12-08-2004, 10:48 AM
    • South Dakota
    • Posts 38
    I am trying to implement a custom sort for my gridview, but in the sorting event, the e.SortDirection is always ascending. Here is some code...


    void gridview1_Sorting(object sender, GridViewSortEventArgs e)
    {
    if (e.SortDirection == SortDirection.Ascending)
    {
    // sort e.SortExpression ascending
    }
    else
    {
    // sort e.SortExpression descending
    }
    }


    Unfortunately every time I click on the column header, e.SortDirection doesn't change. I would expect it to be ascending the first time I click on it, but the second time I click it (the same column) is ascending as well. Am I doing something wrong?

    Thanks.

    -- chad
  • Re: GridView e.SortDirection always ascending

    02-11-2005, 1:58 PM
    • Contributor
      2,680 point Contributor
    • phuff
    • Member since 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • AspNetTeam
    The GridView will switch the SortDirection to be Descending only if the new SortExpression that it gets from the field SortExpression is the same as the current GridView.SortExpression and GridView.SortDirection is Ascending. So, if you change the SortExpression in your Sorting handler, it will never be the same as the SortExpression on the field. The good news is that you can do your own comparison and set the SortExpression and SortDirection in the event args in the Sorting handler.

    Hope this helps...
    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: GridView e.SortDirection always ascending

    08-09-2005, 10:15 AM
    • Participant
      972 point Participant
    • Ninjamyst
    • Member since 07-26-2005, 4:58 PM
    • Posts 197
     phuff wrote:
    The GridView will switch the SortDirection to be Descending only if the new SortExpression that it gets from the field SortExpression is the same as the current GridView.SortExpression and GridView.SortDirection is Ascending. So, if you change the SortExpression in your Sorting handler, it will never be the same as the SortExpression on the field. The good news is that you can do your own comparison and set the SortExpression and SortDirection in the event args in the Sorting handler. Hope this helps...


    I am not changing the sortexpression anywhere and the sortdirection is always Ascending....

    Edit:  After doing more googling, it seems the GridView will change the SortDirection only when binding to a datasource object....
  • Re: GridView e.SortDirection always ascending

    10-31-2005, 8:56 AM
    • Member
      20 point Member
    • siggy
    • Member since 10-31-2005, 1:54 PM
    • Posts 4
    I'm struggling with the same problem at the moment.

    How is it possible to 'set the SortExpression and SortDirection' when these are read-only variables?

    Cheers

    Siggy

    PS.  I ended up using some code from another thread and basically creating my own flip-flop for asc or desc.
    I'm new to asp.net so my example code included here for others my be way off base, but if anyone else can provide a better answer.... :o)

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["sortColumn"] = " ";
                ViewState["sortDirection"] = " ";
            }
        }

    protected void gvResults_Sorting(object sender, GridViewSortEventArgs e)
        {
            string strSearch = tbFind.Text;
            string sortDir;

            if(ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
            {
                if ("ASC" == ViewState["sortDirection"].ToString())
                {
                    ViewState["sortDirection"] = "DESC";
                    //gvResults.SortDirection = SortDirection.Descending;
                }
                else
                {
                    ViewState["sortDirection"] = "ASC";
                    //gvResults.SortDirection = SortDirection.Ascending;
                }        
            }
            else
            {
                ViewState["sortColumn"] = e.SortExpression.ToString();
                ViewState["sortDirection"] = "ASC";
            }
             // I need to take into account multi words search, as not done here!!!!
            string cmdStr = "SELECT Author, Year, Title,  Creator, Publication, Description, URI FROM Archive "
                + "WHERE Author LIKE '%" + strSearch + "%' "
                + "OR Title LIKE '%" + strSearch + "%' "
                + "OR Keywords LIKE '%" + strSearch + "%' "
                + "ORDER BY " + ViewState["sortColumn"].ToString() + " " + ViewState["sortDirection"].ToString();

            BindData(cmdStr);
        }

    void BindData(string sqlCmd)
        {

            ///////////////////////////////////////////////////
            // as I'm manually binding dataset to the gridview
            // I need to overload the pageindexchanging & sorting
            // methods.  Using the SqlDataSource wouldn't work
            // here as we're using dynamic sql command based on use input!
            //////////////////////////////////////////////////

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PortlandDARConnectionString"].ToString());
           
            SqlDataAdapter da = new SqlDataAdapter(sqlCmd, conn);
            ds = new DataSet();

            da.Fill(ds, "Results");

            gvResults.DataSource = ds;
            gvResults.DataBind();
        }

  • Re: GridView e.SortDirection always ascending

    10-31-2005, 1:56 PM
    • Contributor
      2,680 point Contributor
    • phuff
    • Member since 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • AspNetTeam
    SortExpression and SortDirection are get/set on GridViewSortEventArgs, which are the event args passed to you in the Sorting event.
    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: GridView e.SortDirection always ascending

    11-01-2005, 5:28 AM
    • Member
      20 point Member
    • siggy
    • Member since 10-31-2005, 1:54 PM
    • Posts 4
    Ok so I'm clearly missing something here!

    I have a gridView to which I've got a member function assigned to it's sorting event.

    when I click the header title to do a sort and drop into this function I get
    e.SortDirection - which is always ascending &
    e.SortExpression - which is the title of the column I've just clicked on
    gv.SortExpression - is always empty / blank
    gv.SortDirection - is always ascending

    So how would I go about getting the correct flip-flop of asc / desc for the gridview columns using the gridView or GridViewSortEventArgs member variables without using my own ViewState variables as detailed in my example solution?

    Do you mean that I should replicate the gridView logic (as detailed in your first post) using the gv & e sortExp / sortDir member variables?  Then use e.SortDirection, once I've determined (and set) it's correct direction?

    If so, I'm still confused as gv.sortExp / sortDir never seem to get updated (they are always blank & ascending) and as these are readonly I can't update them!

    Thanks in advance

    Siggy



  • Re: GridView e.SortDirection always ascending

    11-01-2005, 10:00 AM
    • Participant
      972 point Participant
    • Ninjamyst
    • Member since 07-26-2005, 4:58 PM
    • Posts 197
    I think the sortdirection only works if you set up your gridview with a datasource or something.  The gridview isn't all that friendly when you don't use it EXACTLY the way they want you to use it......You are better off storing a custom sortdirection in viewstate than fight with the gridview to do what you want.
  • Re: GridView e.SortDirection always ascending

    11-01-2005, 10:29 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs
    hello.

    i think you're suppposed to set the e.SortDirection and not the gv.sprtexpression (ie, inside the event you're supposed to set the sortdirection and sortexpression properties of the eventargs parameter passed to the method. those values will then be passed to the grid).


    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: GridView e.SortDirection always ascending

    11-01-2005, 11:24 AM
    • Participant
      972 point Participant
    • Ninjamyst
    • Member since 07-26-2005, 4:58 PM
    • Posts 197
     Luis Abreu wrote:
    hello.

    i think you're suppposed to set the e.SortDirection and not the gv.sprtexpression (ie, inside the event you're supposed to set the sortdirection and sortexpression properties of the eventargs parameter passed to the method. those values will then be passed to the grid).




    But in order to set the e.SortDirection, we need some way to remember the previous sort direction for that column.  So no matter what, we end up storing the sortdirection ourself....or am I completely off?
  • Re: GridView e.SortDirection always ascending

    11-01-2005, 11:30 AM
    • Member
      20 point Member
    • siggy
    • Member since 10-31-2005, 1:54 PM
    • Posts 4
    Hi

    Yes I agree Ninjamyst.  This is what I've found.
    I've got sorting working to my satisfaction using ViewState, but was just trying clarify phuff's posts / directions.

    Luis, yes I realise that I can set the gv.sortExp / Dir member vars, but I don't see what use this is.
    Especially as gv.sortExp / Dir never seem to offer any meaningful data which I could use in determining what to set the e.sortExp / Dir too!?!?  That is gv.sortExpression is always blank & gv.sortDirection is always ascending.

    Anyway, as I said the example solution I arrived at (via another threads code / suggestion) seems to work fine, I was just trying to resolve the 'correct' way of doing things (as offered by phuff) in my mind.

    Thanks all in advance

    Siggy

  • Re: GridView e.SortDirection always ascending

    11-01-2005, 11:47 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs
    hello.

    yes, in this case you're responsible for making it work correctly (ie, you must use viewstate or another state mechanism to keep state between requests)

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: GridView e.SortDirection always ascending

    11-01-2005, 12:24 PM
    • Contributor
      2,680 point Contributor
    • phuff
    • Member since 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • AspNetTeam
    Luis and others are correct- if you're using DataSource (rather than DataSourceID), sorting works much the same way it did in DataGrid.  You will get the SortExpression for the header you clicked on in the event args, but it's up to you to use the SortExpression to find the correct SortDirection.  SortDirection is largely for use with GridViews that use DataSourceID.
    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: GridView e.SortDirection always ascending

    11-03-2005, 4:32 AM
    • Member
      20 point Member
    • siggy
    • Member since 10-31-2005, 1:54 PM
    • Posts 4
    Many thanks forthe clarification guys.
    It's much appreciated.

    Siggy

  • Re: GridView e.SortDirection always ascending

    11-04-2005, 5:39 PM
    • Contributor
      3,432 point Contributor
    • erdsah88
    • Member since 12-12-2004, 11:18 AM
    • ISTANBUL
    • Posts 900

    I am  really confused!!!!

    if viewstate only thing that works for a
    the gridview is  binded to a generic datatable
    Satılık,Kiralık emlak ilanlari
  • Re: GridView e.SortDirection always ascending

    07-25-2008, 7:01 PM
    • Member
      10 point Member
    • hooplafoo
    • Member since 01-24-2007, 10:41 PM
    • Posts 17

     quick and dirty, but it works, I needed it fast, so this works.. handles sorting based on selected datafield... since I only had two cols no biggie, but this could be a real pain... but storing sort direction is on the developer to figure out when using LINQ stuff... I'd love to see a better working example

     

    switch(e.SortExpression)

    {

    case "field1":if (Session["gSort"].ToString() == "desc")

    {

    tUHits =
    from p in db.table

    orderby p.column ascending

    select p;

    Session["gSort"] = "asc";

    }

    else {

    tUHits = from p in db.table

    orderby p.column descending

    select p;Session["gSort"] = "desc";

    }

     

    break;

    case "field2":

    if (Session["gSort"].ToString() == "desc")

    {

    tUHits =
    from p in db.table

    orderby p.column ascending

    select p;Session["gSort"] = "asc";

    }

    else

    {

    tUHits =
    from p in db.table

    orderby p.column descending

    select p;Session["gSort"] = "desc";

    }

    break;

    default: tUHits =from p in db.table

    orderby p.column descending

    select p;break;

    }

     

     

    .Net<3.5> HardCore<commando>
    Filed under: ,
Page 1 of 2 (17 items) 1 2 Next >