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 6742511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:51:07+00:00 2026-05-26T11:51:07+00:00

ASP.NET GridView sorting: I want to change the sorting direction from ascending to descending

  • 0

ASP.NET GridView sorting: I want to change the sorting direction from ascending to descending when I click the column name. But by default, sorting direction always showing ascending. Am I missing any property setting to toggle sorting direction? Any help greatly appreciated. Thanks.

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{

    DataTable dataTable = myDataTable(strSQL);

    if (dataTable != null)
    {

        DataView dataView = new DataView(dataTable);

        dataView.Sort = e.SortExpression + " " + SortDirection(e.SortDirection);

        gridView.DataSource = dataView;

        gridView.DataBind();

     }

}
//e.SortDirection always showing ascending

aspx gridview column design for sorting:

<asp:BoundField DataField="CREATE_DATE" HeaderText="Create Date" SortExpression="CREATE_DATE" />
  • 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-26T11:51:08+00:00Added an answer on May 26, 2026 at 11:51 am

    Did you try setting the AllowSorting attribute of the GridView to true in the markup?

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True">
        <Columns>
            ...
        </Columns>
    </asp:GridView>
    

    Or, you could do it in your code behind after the DataBind:

    GridView1.AllowSorting = true;
    

    This attribute allows the user to click on the column headers to sort on that column.

    Edit: You actually have to create logic to decide whether to sort ascending or descending (e.SortDirection will only help you if your using a “data source control” like SQLDataSource). Then you can just change your dataView.Sort code to some like this:

    if(someCondition)
        dataView.Sort = e.SortExpression + " DESC";
    else
        dataView.Sort = e.SortExpression + " ASC";
    

    Where someCondition is your logic for deciding which way to sort (for instance you could keep track of what the last sort direction was for that column, then do the opposite if the user clicks it again).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET GridView with a column mapped to a boolean. I want
I am a beginner to asp.net. I want to sort a gridview but the
The ASP.NET GridView control's default column types just don't seem to be up to
I have an ASP.Net GridView, with two columns (ID and name). The name column
When using DataSources in ASP.net applications, paging and sorting along with GridView only works
I'm converting functionality from an asp.net Gridview to a Listview. In the gridview when
Disable a post back from asp.net i.e. buttons, links, gridview page index changing and
I am using C#,ASP.NET I have a Gridview for which I have provided Sorting,
I have a GridView with a row number (Container.DataItemIndex) column in ASP.NET (1,2,3,4, ...)
I have developed an asp.net application that has a Gridview control. I want it

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.