|
|
|
|
|
|
|
Page Views :
|
5780
|
|
Downloads :
|
255
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
by Andrew Fenster and Boguslaw Wojcieszyk
DESCRIPTION:
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.
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.

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.

The code to create this grid is in the attached project. The best way to understand how it works is to download the project and run it. Here's a brief explanation:
The Markup
Here is the markup for the GridView.
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.

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

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.

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.

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.

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

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.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
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.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|