Click here to Skip to main content
Click here to Skip to main content

Sorting with Up and Down icons

, 19 Apr 2010
Rate this:
Please Sign up or sign in to vote.
1. First we need to create two variables in ViewState to store current direction and expression:public string CurrentSortExpression { get { if (ViewState["sortExpression"] != null) { return...
1. First we need to create two variables in ViewState to store current direction and expression:

public string CurrentSortExpression
{
get
{
if (ViewState["sortExpression"] != null)
{
return ViewState["sortExpression"].ToString();
}
else
{
ViewState["sortExpression"] = string.Empty;
return string.Empty;
}
}
set
{
ViewState["sortExpression"] = value;
}
}
 
public string CurrentSortDirection
{
get
{
if (ViewState["sortDirection"] != null)
{
return ViewState["sortDirection"].ToString();
}
return string.Empty;
}
set
{
ViewState["sortDirection"] = value;
}
}

 
2. Then we have to implement handler to attach sort icons to header rows:

protected void GridViewSortImages(object sender, GridViewRowEventArgs e)
{
GridView senderGridView = (GridView)sender;
Literal space = new Literal();
space.Text = " ";
if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
{
for(int i=0;i<gvusers.columns.count;i++)>
{
TableCell cell = e.Row.Cells[i];
DataControlField column=gvUsers.Columns[i];

if (cell.HasControls())
{
LinkButton button = cell.Controls[0] as LinkButton;
if (button != null)
{
Image image = new Image();
image.ImageUrl = "~/AdminUI/Images/icon-sort_default.gif";
if (CurrentSortExpression==column.SortExpression)
{
if (CurrentSortDirection=="asc")
image.ImageUrl = "~/AdminUI/Images/icon-sort1.gif";
else if (CurrentSortDirection=="desc")
image.ImageUrl = "~/AdminUI/Images/icon-sort2.gif";
}
cell.Controls.Add(image);
}
}
}
}
}

 
3. Farther let's make some logic in Sorting handler for our GridView:

protected void gvUsers_Sorting(object sender, GridViewSortEventArgs e)
{
if (CurrentSortExpression == e.SortExpression.ToString())
{
if (CurrentSortDirection == "asc")
CurrentSortDirection = "desc";
else
CurrentSortDirection = "asc";
}
else
{
CurrentSortExpression = e.SortExpression.ToString();
CurrentSortDirection = "asc";
}
 
BindUsers(0, CurrentSortExpression, (CurrentSortDirection == "asc")?true:false);
}

 
4. And at last lets use GridViewSortImages in RowCreated event:

protected void gvUsers_RowCreated(object sender, GridViewRowEventArgs e)
{
// Some code
GridViewSortImages(sender, e);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pavel Yermalovich
Software Developer (Senior)
Belarus Belarus
Software developer with over 3 years of extensive experience in analysis, design and development. Recently, most interested in refactoring and design patterns applied to .NET Framework.
 
Core technologies I am using: OOP, OOD, DDD, TDD, N-tier applications, enterprise development.
 
Certificates:
Brainbench: .NET Framework 3.5 Fundamentals, Data Modeling Concepts, Web Design Concepts, C#
 
Microsoft: Exam 70-526: TS: Microsoft .NET Framework 2.0 - Windows-Based Client Development
 
INTUIT.RU: Development of Web-application ASP. NET Using Visual Studio. NET, Web applications in ASP.NET, Microsoft .NET Framework Distributed Applications Development.
 
Specialties
NET Framework : 2.0, 3.5, 4.0
Languages and technologies: C#, ASP.NET MVC 2, ASP.NET MVC 3, WCF, ASP.NET 4.0, Web Services, ADO.NET, LINQ, Entity Framework, NHibernate 3.0, JavaScript, HTML, CSS, XML, Ajax
RDBMS : SQL Server 2005, 2008, 2008 R2, MS Access.
Reporting: MS SQL Reporting Services, Crystal Reports

Comments and Discussions

 
GeneralCan you please format the code? Also, it will be Awesome if ... PinmvpKunalChowdhury31-Aug-10 0:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 19 Apr 2010
Article Copyright 2010 by Pavel Yermalovich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid