Click here to Skip to main content
15,120,582 members
Articles / Web Development / ASP.NET
Posted 14 Jul 2013

Tagged as


28 bookmarked

Gridview Sorting using attribute [Source Code]

Rate me:
Please Sign up or sign in to vote.
3.80/5 (11 votes)
30 Aug 2013CPOL
Sorting GridView using attribute property of GridView


Many developers who try to sort a GridView generally use the ViewState for maintaining the last position of the GridView. Here, I want to show you a new technique of doing this...

Here is my GridView code:

<asp:GridView ID="GridView1" 
runat="server" AllowSorting="true" 
        AutoGenerateColumns="False" onsorting="GridView1_Sorting" 
        CurrentSortField="employeeid" CurrentSortDirection="ASC" 
        onrowcreated="GridView1_RowCreated" AllowPaging="true" 
        CaptionAlign="Bottom" onpageindexchanging="GridView1_PageIndexChanging" 
        <asp:BoundField  DataField="EmployeeId" HeaderText="Last Name"  
              ItemStyle-Width="15%" SortExpression="EmployeeId" >
        <ItemStyle Width="15%"></ItemStyle>
        <asp:BoundField DataField="Name" 
        HeaderText="First Name"  ItemStyle-Width="15%" 
              SortExpression="Name"  >
             <ItemStyle Width="15%"></ItemStyle>
        <asp:BoundField DataField="gender" 
        HeaderText="Email" ItemStyle-Width="15%" >
            <ItemStyle Width="15%"></ItemStyle>
        <asp:TemplateField HeaderText="Change Password"  


            <asp:LinkButton ID="imgbtn1" 
            runat="server">change password</asp:LinkButton>

             <ItemStyle Width="15%"></ItemStyle>
        <asp:BoundField DataField="city" 
        HeaderText="Date created"  ItemStyle-Width="15%">
             <ItemStyle Width="15%"></ItemStyle>
        <PagerSettings Mode="NextPreviousFirstLast" />

And I sort using the attribute property of the GridView.

See here, through attribute, I am maintaining last sorted record. Suppose last I sort asc then asc and if I last sorted desc then desc. This is how it works:

private void GridView1_Sorting (GridView gridview, GridViewSortEventArgs e, 
         out SortDirection sortDirection, out string sortField)
    sortField = e.SortExpression;
    sortDirection = e.SortDirection;
    if (gridview.Attributes["CurrentSortField"] != null && 
         gridview.Attributes["CurrentSortDirection"] != null)
        if (sortField == gridview.Attributes["CurrentSortField"])
            if (gridview.Attributes["CurrentSortDirection"] == "ASC")
                sortDirection = SortDirection.Descending;
                sortDirection = SortDirection.Ascending;
        gridview.Attributes["CurrentSortField"] = sortField;
        gridview.Attributes["CurrentSortDirection"] = 
           (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");

Now you will get the last sorted and filed easily and it generally shows that while developing, there is too much load on the page using the viewstate.

Code Description

In sorting, you have two options asc/desc.

Now, if you are sorting by asc, then in gridview attribute add to CurrentSortDirection= asc.

When user clicks for the second time, we get value from CurrentSortDirection asc so now do desc and add CurrentSortDirection =desc.

Simple, but very useful.

I am sure this will definitely help you. If you still did not get it, take a look at the source code.


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


About the Author

Dholakiya Ankit
Web Developer
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 2 Pin
ridoy26-Aug-13 5:28
professionalridoy26-Aug-13 5:28 
GeneralRe: My vote of 2 Pin
Dholakiya Ankit26-Aug-13 18:27
MemberDholakiya Ankit26-Aug-13 18:27 
GeneralRe: My vote of 2 Pin
Dholakiya Ankit26-Aug-13 22:29
MemberDholakiya Ankit26-Aug-13 22:29 
GeneralRe: My vote of 2 Pin
ridoy27-Aug-13 2:15
professionalridoy27-Aug-13 2:15 
GeneralRe: My vote of 2 Pin
Dholakiya Ankit27-Aug-13 2:38
MemberDholakiya Ankit27-Aug-13 2:38 
GeneralMy vote of 3 Pin
Manikandan Sekar21-Aug-13 23:11
professionalManikandan Sekar21-Aug-13 23:11 
GeneralRe: My vote of 3 Pin
Dholakiya Ankit22-Aug-13 19:00
MemberDholakiya Ankit22-Aug-13 19:00 
QuestionVote of 5 Pin
Leslie K.15-Aug-13 11:27
MemberLeslie K.15-Aug-13 11:27 
AnswerRe: Vote of 5 Pin
Dholakiya Ankit15-Aug-13 18:22
MemberDholakiya Ankit15-Aug-13 18:22 
Questiondemo Pin
Dholakiya Ankit13-Aug-13 19:49
MemberDholakiya Ankit13-Aug-13 19:49 
AnswerRe: demo Pin
Joezer BH13-Aug-13 23:04
professionalJoezer BH13-Aug-13 23:04 
GeneralRe: demo Pin
Dholakiya Ankit13-Aug-13 23:16
MemberDholakiya Ankit13-Aug-13 23:16 
QuestionMy Vote of 3 Pin
SruthiR13-Aug-13 19:34
MemberSruthiR13-Aug-13 19:34 
AnswerRe: My Vote of 3 Pin
Dholakiya Ankit13-Aug-13 19:36
MemberDholakiya Ankit13-Aug-13 19:36 
AnswerRe: My Vote of 3 Pin
Dholakiya Ankit14-Aug-13 0:07
MemberDholakiya Ankit14-Aug-13 0:07 
GeneralRe: My Vote of 3 Pin
SruthiR14-Aug-13 1:55
MemberSruthiR14-Aug-13 1:55 
GeneralRe: My Vote of 3 Pin
Dholakiya Ankit14-Aug-13 2:20
MemberDholakiya Ankit14-Aug-13 2:20 
GeneralMy vote of 3 Pin
AlexCode6-Aug-13 21:56
professionalAlexCode6-Aug-13 21:56 
GeneralRe: My vote of 3 Pin
Dholakiya Ankit6-Aug-13 22:04
MemberDholakiya Ankit6-Aug-13 22:04 
GeneralMy vote of 1 Pin
Mehedi Hassan Mody5-Aug-13 23:32
MemberMehedi Hassan Mody5-Aug-13 23:32 
GeneralRe: My vote of 1 Pin
Dholakiya Ankit5-Aug-13 23:46
MemberDholakiya Ankit5-Aug-13 23:46 
GeneralMy vote of 3 Pin
HaBiX4-Aug-13 4:45
MemberHaBiX4-Aug-13 4:45 
GeneralRe: My vote of 3 Pin
Dholakiya Ankit4-Aug-13 19:12
MemberDholakiya Ankit4-Aug-13 19:12 
GeneralRe: My vote of 3 Pin
prakash.padariya20-Jan-14 20:18
Memberprakash.padariya20-Jan-14 20:18 
GeneralRe: My vote of 3 Pin
Dholakiya Ankit3-Feb-14 16:42
MemberDholakiya Ankit3-Feb-14 16:42 

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.