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

  • Home
  • SEARCH
  • 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 106087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:32:29+00:00 2026-05-11T01:32:29+00:00

I have a gridview and I need to sort its elements when the user

  • 0

I have a gridview and I need to sort its elements when the user clicks on the header.
Its datasource is a List object.

The aspx is defined this way :

<asp:GridView ID='grdHeader' AllowSorting='true' AllowPaging='false'      AutoGenerateColumns='false' Width='780' runat='server'  OnSorting='grdHeader_OnSorting' EnableViewState='true'>     <Columns>         <asp:BoundField DataField='Entitycode' HeaderText='Entity' SortExpression='Entitycode' />         <asp:BoundField DataField='Statusname' HeaderText='Status' SortExpression='Statusname' />         <asp:BoundField DataField='Username' HeaderText='User' SortExpression='Username' />     </Columns> </asp:GridView> 

The code behind is defined this way :
First load :

protected void btnSearch_Click(object sender, EventArgs e) {     List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();     this.grdHeader.DataSource = items;     this.grdHeader.DataBind(); } 

when the user clicks on headers :

protected void grdHeader_OnSorting(object sender, GridViewSortEventArgs e) {     List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();     items.Sort(new Helpers.GenericComparer<V_ReportPeriodStatusEntity>(e.SortExpression, e.SortDirection));     grdHeader.DataSource = items;     grdHeader.DataBind(); } 

My problem is that e.SortDirection is always set to Ascending.
I have webpage with a similar code and it works well, e.SortDirection alternates between Ascending and Descending.

What did I do wrong ?

  • 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. 2026-05-11T01:32:30+00:00Added an answer on May 11, 2026 at 1:32 am

    You can use a session variable to store the latest Sort Expression and when you sort the grid next time compare the sort expression of the grid with the Session variable which stores last sort expression. If the columns are equal then check the direction of the previous sort and sort in the opposite direction.

    Example:

    DataTable sourceTable = GridAttendence.DataSource as DataTable; DataView view = new DataView(sourceTable); string[] sortData = ViewState['sortExpression'].ToString().Trim().Split(' '); if (e.SortExpression == sortData[0]) {     if (sortData[1] == 'ASC')     {         view.Sort = e.SortExpression + ' ' + 'DESC';         this.ViewState['sortExpression'] = e.SortExpression + ' ' + 'DESC';     }     else     {         view.Sort = e.SortExpression + ' ' + 'ASC';         this.ViewState['sortExpression'] = e.SortExpression + ' ' + 'ASC';     } } else {     view.Sort = e.SortExpression + ' ' + 'ASC';     this.ViewState['sortExpression'] = e.SortExpression + ' ' + 'ASC'; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Add the following to your class, e.g. in its constructor:… May 11, 2026 at 9:36 am
  • added an answer int() is the Python standard built-in function to convert a… May 11, 2026 at 9:36 am
  • added an answer Keep track of the predecessor nodes. In the easiest implementation,… May 11, 2026 at 9:36 am

Related Questions

I have a gridview and I need to sort its elements when the user
I have a ASP.NET GridView that uses template columns and user controls to allow
I'm using the ASP net Ajax toolkit and have a GridView within the UpdatePanel
I have an ASP.NET GridView with a DropDownList (in an Item Template) and a
I have a GridView control in an Asp.net application, that has a <asp:buttonField> of
I have a GridView where one column is bound to an object property containing
I have a GridView defined like this: <asp:GridView ID=myGridView ruant=server> <asp:BoundField DataField=myField /> <asp:CommandField
I have a gridview like below: <asp:GridView DataKeyNames=TransactionID AllowSorting=True AllowPaging=TrueID=grvBrokerage runat=server AutoGenerateColumns=False CssClass=datatable Width=100%
I have a GridView that I would like to bind to an ObjectDataSource. I
I have a GridView that allows editing the values in every column, in every

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.