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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:36:03+00:00 2026-06-02T23:36:03+00:00

My SortedAscendingHeaderStyle and SortedDescendingHeaderStyle is not working at all <asp:GridView ID=grdProducts runat=server CssClass=grid AllowPaging=True

  • 0

My SortedAscendingHeaderStyle and SortedDescendingHeaderStyle is not working at all

<asp:GridView ID="grdProducts" runat="server" CssClass="grid" AllowPaging="True" AllowSorting="True" PageSize="100" EmptyDataText="No data to show"
              onrowdatabound="grdProducts_RowDataBound"  onrowediting="grdProducts_RowEditing" onsorting="grdProducts_Sorting" AutoGenerateEditButton="True">
  <AlternatingRowStyle CssClass="even" />
  <SortedAscendingHeaderStyle ForeColor="White" CssClass="sorted" />
  <SortedDescendingHeaderStyle CssClass="sorted desc" />
</asp:GridView>

Rows are sorted correctly when headers are clicked, but when I inspect the header using FireBug, it only shows: (this is when sorted ascending)

<th scope="col">
  <a href="javascript:__doPostBack('ctl00$body$ctl00$grdProducts','Sort$Namekey')">Namekey</a>
</th>

ForeColor and CssClass are not set at all.

Anyone has any idea what I am doing wrong?

EDIT: My C# code behind

  protected void grdProducts_Sorting(object sender, GridViewSortEventArgs e)
  {
    if ((string)ViewState["SortColumn"] == e.SortExpression)
      ViewState["SortDirection"] = ((string)ViewState["SortDirection"] == "") ? " DESC" : "";
    else
    {
      ViewState["SortColumn"] = e.SortExpression;
      ViewState["SortDirection"] = "";
    }
  }

  protected override void OnPreRender(EventArgs e)
  {
    BindGrid();
    base.OnPreRender(e);
  }

  private void BindGrid()
  {
    string query = "SELECT ... ORDER BY " + ViewState["SortColumn"] + ViewState["SortDirection"];

    DataTable dt = SqlFunctions.Select(query);
    grdProducts.DataSource = dt;
    grdProducts.DataBind();
  }
  • 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-06-02T23:36:05+00:00Added an answer on June 2, 2026 at 11:36 pm

    I’m not sure if SortedDescendingHeaderStyle works without code if you’re not using an asp:SQLDataSource as your GridView data source. But a little coding can get you there.

    You need to apply the CSS style manually to the header cell. You can do it in the Sorting event.

    protected void grdProducts_Sorting(object sender, GridViewSortEventArgs e)
    {
       if ((string)ViewState["SortColumn"] == e.SortExpression)
       {
          ViewState["SortDirection"] = ((string)ViewState["SortDirection"] == "") ? " DESC" : "";
          grdProducts.HeaderRow.Cells[GetColumnIndex( e.SortExpression )].CssClass = "AscendingHeaderStyle";
       }
       else
       {
          ViewState["SortColumn"] = e.SortExpression;
          ViewState["SortDirection"] = "";
          grdProducts.HeaderRow.Cells[GetColumnIndex( e.SortExpression )].CssClass = "DescendingHeaderStyle";
       }
       BindGrid();
    }
    
    
    
    
    private int GetColumnIndex( string SortExpression )
    {
        int i = 0;
        foreach( DataControlField c in gvwCustomers.Columns )
        {
            if( c.SortExpression == SortExpression )
                break;
            i++;
        }
        return i;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview with the following markup: <asp:GridView ID=gv runat=server Width=700px skinid=gridview HeaderStyle-HorizontalAlign=Left
I'm new to Asp.net and I'm currently working with GridViews. I've looked around this
I have a GridView , which is bound to a database table. This GridView
I am a new ASP.NET developer and now I am having an issue in
I have created a gridview using toolbox in c#, it is able to show
I am making a gridview which uses scrolling as the column are a more
Hey I have a gridView and I would like to extract the PhotoPath (ftppath
I have a grid view in which I want to show images. I am
I have a gridview with autogenerated columns that I set programmatically I want to
Hey, I realize that pages are just going to look different in varying browsers,

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.