Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:05:27+00:00 2026-05-26T00:05:27+00:00

The default sorting order on the gridview control is ascending first, then descending. Now

  • 0

The default sorting order on the gridview control is ascending first, then descending. Now below I have code to change that around. So far so good. But when I click on another column again, it sets the sort direction to ascending again. It alternates between descending and ascending on every click, no matter which column. Now what I really want is for the first click on any column to be descending, and if I click any column for the second time(second time meaning consecutively), it should be ascending. Example: I have 2 columns, one is salary and the other is age. Now I click on salary, and the first sorting direction is descending, not the default ascending(that’s what the code does). Now when I click on age, it switches the sorting direction to ascending, I want it to stay the same when I change to another column, BUT should I click on salary again for the second time, it should switch to ascending(cause descending was the first click). Any suggestions?

My code:

public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null) 
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set
    {
        ViewState["sortDirection"] = value; 
    }
}
protected void GridView1_OnSorting(object sender, GridViewSortEventArgs e)
{
    if (ViewState["sortDirection"] == null)
    {
        e.SortDirection = SortDirection.Descending;
    }
    else if (GridViewSortDirection == SortDirection.Ascending)
    {
        e.SortDirection = SortDirection.Descending;
    }
    else if (GridViewSortDirection == SortDirection.Descending)
    {
        e.SortDirection = SortDirection.Ascending;
    }

    GridViewSortDirection = e.SortDirection;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:05:28+00:00Added an answer on May 26, 2026 at 12:05 am

    Try checking to determine whether the SortExpression has changed from the previously set SortExpression and if so default the SortDirection to Descending.

    protected void GridView1_OnSorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression != GridView1.SortExpression)
        {
            e.SortDirection = SortDirection.Descending;
            GridViewSortDirection = e.SortDirection;
        }
        else if (GridViewSortDirection == SortDirection.Ascending)
        {
            e.SortDirection = SortDirection.Descending;
        }
        else if (GridViewSortDirection == SortDirection.Descending)
        {
            e.SortDirection = SortDirection.Ascending;
        }
    
        GridViewSortDirection = e.SortDirection;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that I have a dropdownlist with 2 items and, by default, the first
Has anyone managed to extend a GridView with default sorting? This seems pretty trivial
I have a JQGrid populated with data working correctly. The default sorting functionality is
up till now I have a linq query that fills a datagrid perfectly with
I want the default sorting to be rating DESC, but I also have, some
I wrote code below that is very similar to the accepted answer here .
By default each row of a Gridview maps to each row in a datatable
I have a problem getting sorting to work with multiple joined tables. For example:
I have several huge sorted enumerable sequences that I want to merge . Theses
I encountered an interesting thing today that I have never noticed before. It appears

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.