SIGN UP MEMBER LOGIN:    
ARTICLE

GridView Sorting

Posted by Andrew Fenster Articles | ASP.NET Controls in C# September 29, 2010
We will describe a GridView sorting method we used in recent projects. Each GridView column can be sorted in ascending or descending order. An arrow indicates which column is being sorted, and in which direction.
Reader Level:
Download Files:
 

by Andrew Fenster and Boguslaw Wojcieszyk 

We're going to describe a GridView sorting method we used many times in recent projects.  Here are two images which show what it looks like.  

In the first image, the first column (UserName) is sorted in ascending order.  There is an arrow pointing up to indicate the sort column and sort direction.

1.gif

In the second image, the second column (Name) is sorted in descending order.  Again, the arrow indicates the sort column and direction.  We can sort any column by clicking on the header.  We can reverse the sort direction by clicking again.  The arrow shows which column is being sorted and in which direction.

2.gif 

The code to create this grid is in the attached project.  Here's a brief explanation:

The Markup

Here is the markup for the GridView.  

 3.gif

Each column includes a HeaderTemplate.  The HeaderTemplate contains a LinkButton.  A LinkButton is a button that looks like a hyperlink.  We set Font-Underline=False, so the links look like plain text, but that is a matter of preference.  The important thing to understand is that the header for every column looks like text, but is really a button.  When the button gets clicked, the gridUsers_Sort method gets called.

The Code
We define sort order as an enum.

4.gif 

When the page loads, we determine the grid sort order and the current sort column.  These values are stored in the ViewState.

5.gif 

When we're done processing, and the page is about to render, we save the sort order and sort column back into the ViewState.  Then we call PositionGlyph() to put the sort arrow into the grid's header.

6.gif 

PositionGlyph() creates an Image control.  If the sort order is ascending, the Image control will have an image of an arrow pointing up.  If the sort order is descending, the Image control will have an image of an arrow pointing down.  The Image control gets inserted in the column being sorted.

7.gif 

When the user clicks the LinkButton at the top of a column, the gridUsers_Sort() method gets called.  We determine which column is being sorted by looking at the LinkButton's CommandArgument.  We update the current sort order and sort column and then we bind the grid.

8.gif 

Whenever we bind the GridView, we sort the data first.

9.gif 

Going Further

Most of this code is the same for any grid you want to sort.  The Page_Load(), Page_PreRender(), PositionGlyph() and grid_Sort() methods don't change.  Rather than cut and paste these methods over and over, they could all go in a base class.  

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

how to get the above code

Posted by zahid hazoor Mar 29, 2012

very helpfull

Posted by carlos calvo Jul 11, 2011

The way it sorts in my example doesn't matter so much.  If you make a grid, you'll be displaying your own data, not my data.  My data is a List of User objects.  Your data will be something else.  Furthermore, your data might be in some other format, like a DataSet or DataTable.  You'll have to write your own sorting code in the PopulateUserGrid() method, and it will behave the way you wrote it.  


In other words, my code allows you to know which column to sort and whether to sort it ascending or descending.  You still have to write your own code to do the actual sorting.

To answer your question: in MY code, if two rows have the same value, they will display in the order they came from the data source.  Whatever order they came out of the database is the order they will be displayed.  

Posted by Andrew Fenster Oct 06, 2010

what if there are two rows with same "UserName","Name","Role".How that will be sorted.

Posted by mark dinesh Oct 06, 2010

You can sort the rows however you want.  Take a look at the PopulateUserGrid( ) method.  You know which column the user wants sorted.  You know whether the user wants the data sorted ascending or descending.  Then it's up to you to write the code that will do the sorting. 

In the downloadable example, there are several people with the same role.  If you sort by role, they are displayed in the order they came from the data access layer.  If you don't like this behavior, you can always change the code that does the sorting.  

Posted by Andrew Fenster Oct 05, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor