Click here to Skip to main content
15,879,535 members
Articles / Web Development / ASP.NET
Tip/Trick

Sorting with Up and Down icons

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
19 Apr 2010CPOL1 min read 13.2K   2   1
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:
<br />
public string CurrentSortExpression<br />
        {<br />
            get<br />
            {<br />
                if (ViewState["sortExpression"] != null)<br />
                {<br />
                    return ViewState["sortExpression"].ToString();<br />
                }<br />
                else<br />
                {<br />
                    ViewState["sortExpression"] = string.Empty;<br />
                    return string.Empty;<br />
                }<br />
            }<br />
            set<br />
            {<br />
                ViewState["sortExpression"] = value;<br />
            }<br />
        }<br />
<br />
        public string CurrentSortDirection<br />
        {<br />
            get<br />
            {<br />
                if (ViewState["sortDirection"] != null)<br />
                {<br />
                    return ViewState["sortDirection"].ToString();<br />
                }<br />
                return string.Empty;<br />
            }<br />
            set<br />
            {<br />
                ViewState["sortDirection"] = value;<br />
            }<br />
        }<br />


2. Then we have to implement handler to attach sort icons to header rows:
<br />
protected void GridViewSortImages(object sender, GridViewRowEventArgs e)<br />
        {<br />
            GridView senderGridView = (GridView)sender;<br />
            Literal space = new Literal();<br />
            space.Text = "    ";<br />
            if (e.Row != null && e.Row.RowType == DataControlRowType.Header)<br />
            {<br />
                for(int i=0;i<gvusers.columns.count;i++)><br />
                {<br />
                    TableCell cell = e.Row.Cells[i];<br />
                    DataControlField column=gvUsers.Columns[i];<br />
  <br />
                    if (cell.HasControls())<br />
                    {<br />
                        LinkButton button = cell.Controls[0] as LinkButton;<br />
                        if (button != null)<br />
                        {<br />
                            Image image = new Image();<br />
                            image.ImageUrl = "~/AdminUI/Images/icon-sort_default.gif";<br />
                            if (CurrentSortExpression==column.SortExpression)<br />
                            {<br />
                                if (CurrentSortDirection=="asc")<br />
                                    image.ImageUrl = "~/AdminUI/Images/icon-sort1.gif";<br />
                                else if (CurrentSortDirection=="desc")<br />
                                    image.ImageUrl = "~/AdminUI/Images/icon-sort2.gif";<br />
                            }<br />
                            cell.Controls.Add(image);<br />
                        }<br />
                    }<br />
                }<br />
            }<br />
        }<br />


3. Farther let's make some logic in Sorting handler for our GridView:
<br />
protected void gvUsers_Sorting(object sender, GridViewSortEventArgs e)<br />
        {<br />
            if (CurrentSortExpression == e.SortExpression.ToString())<br />
            {<br />
                if (CurrentSortDirection == "asc")<br />
                    CurrentSortDirection = "desc";<br />
                else<br />
                    CurrentSortDirection = "asc";<br />
            }<br />
            else<br />
            {<br />
                CurrentSortExpression = e.SortExpression.ToString();<br />
                CurrentSortDirection = "asc";<br />
            }<br />
<br />
            BindUsers(0, CurrentSortExpression, (CurrentSortDirection == "asc")?true:false);<br />
        }<br />


4. And at last lets use GridViewSortImages in RowCreated event:
<br />
protected void gvUsers_RowCreated(object sender, GridViewRowEventArgs e)<br />
        {<br />
            // Some code<br />
            GridViewSortImages(sender, e);<br />
        }<br />

License

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


Written By
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 ... Pin
Kunal Chowdhury «IN»31-Aug-10 0:19
professionalKunal Chowdhury «IN»31-Aug-10 0:19 

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

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