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 97k
  • Answers 97k
  • 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
  • Editorial Team
    Editorial Team added an answer No. That's not possible with Java ME. May 11, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer My personal opinion is to preferably use the property unless… May 11, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer First, make sure you are using a doctype that will… May 11, 2026 at 7:18 pm

Related Questions

I'm new to MVC, and am not following how you'd do paging and sorting
This is my first real question of need for any of those Gridview experts
I have an ASP.NET GridView which has columns that look like this: | Foo
I am not a big fan of datasets so I use POCO to return

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.