Click here to Skip to main content
13,097,673 members (54,647 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


28 bookmarked
Posted 14 Jul 2013

Sorting GridView using attribute property of GridView

, 14 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Sorting GridView using attribute property of GridView.
This is an old version of the currently published tip/trick.

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 to 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"  ItemStyle-Width="15%">


            <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.

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 and this method works best for us.


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

You may also be interested in...


Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
GeneralMy vote of 2 Pin
ridoy26-Aug-13 4:28
memberridoy26-Aug-13 4:28 
GeneralRe: My vote of 2 Pin
dholakiya ankit26-Aug-13 17:27
memberdholakiya ankit26-Aug-13 17:27 
GeneralRe: My vote of 2 Pin
dholakiya ankit26-Aug-13 21:29
memberdholakiya ankit26-Aug-13 21:29 
GeneralRe: My vote of 2 Pin
ridoy27-Aug-13 1:15
memberridoy27-Aug-13 1:15 
GeneralRe: My vote of 2 Pin
dholakiya ankit27-Aug-13 1:38
memberdholakiya ankit27-Aug-13 1:38 
GeneralMy vote of 3 Pin
Manikandan Sekar21-Aug-13 22:11
memberManikandan Sekar21-Aug-13 22:11 
GeneralRe: My vote of 3 Pin
dholakiya ankit22-Aug-13 18:00
memberdholakiya ankit22-Aug-13 18:00 
QuestionVote of 5 Pin
Leslie K.15-Aug-13 10:27
memberLeslie K.15-Aug-13 10:27 
AnswerRe: Vote of 5 Pin
dholakiya ankit15-Aug-13 17:22
memberdholakiya ankit15-Aug-13 17:22 
Questiondemo Pin
dholakiya ankit13-Aug-13 18:49
memberdholakiya ankit13-Aug-13 18:49 
AnswerRe: demo Pin
Maimonides13-Aug-13 22:04
professionalMaimonides13-Aug-13 22:04 
GeneralRe: demo Pin
dholakiya ankit13-Aug-13 22:16
memberdholakiya ankit13-Aug-13 22:16 
QuestionMy Vote of 3 Pin
SruthiR13-Aug-13 18:34
memberSruthiR13-Aug-13 18:34 
AnswerRe: My Vote of 3 Pin
dholakiya ankit13-Aug-13 18:36
memberdholakiya ankit13-Aug-13 18:36 
AnswerRe: My Vote of 3 Pin
dholakiya ankit13-Aug-13 23:07
memberdholakiya ankit13-Aug-13 23:07 
GeneralRe: My Vote of 3 Pin
SruthiR14-Aug-13 0:55
memberSruthiR14-Aug-13 0:55 
GeneralRe: My Vote of 3 Pin
dholakiya ankit14-Aug-13 1:20
memberdholakiya ankit14-Aug-13 1:20 
GeneralMy vote of 3 Pin
AlexCode6-Aug-13 20:56
professionalAlexCode6-Aug-13 20:56 
GeneralRe: My vote of 3 Pin
dholakiya ankit6-Aug-13 21:04
memberdholakiya ankit6-Aug-13 21:04 
GeneralMy vote of 1 Pin
Mehedi Hassan Mody5-Aug-13 22:32
memberMehedi Hassan Mody5-Aug-13 22:32 
GeneralRe: My vote of 1 Pin
dholakiya ankit5-Aug-13 22:46
memberdholakiya ankit5-Aug-13 22:46 
GeneralMy vote of 3 Pin
HaBiX4-Aug-13 3:45
memberHaBiX4-Aug-13 3:45 
GeneralRe: My vote of 3 Pin
dholakiya ankit4-Aug-13 18:12
professionaldholakiya ankit4-Aug-13 18:12 
GeneralRe: My vote of 3 Pin
prakash.padariya20-Jan-14 19:18
memberprakash.padariya20-Jan-14 19:18 
GeneralRe: My vote of 3 Pin
AJD-salute3-Feb-14 15:42
memberAJD-salute3-Feb-14 15:42 
GeneralMy vote of 5 Pin
Weerayut Teja3-Aug-13 17:35
memberWeerayut Teja3-Aug-13 17:35 
GeneralRe: My vote of 5 Pin
dholakiya ankit4-Aug-13 18:11
professionaldholakiya ankit4-Aug-13 18:11 
GeneralMy vote of 3 Pin
aashi272-Aug-13 0:44
memberaashi272-Aug-13 0:44 
GeneralRe: My vote of 3 Pin
dholakiya ankit2-Aug-13 0:50
professionaldholakiya ankit2-Aug-13 0:50 
QuestionPlease feedfack about this Pin
dholakiya ankit2-Aug-13 0:11
professionaldholakiya ankit2-Aug-13 0:11 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 14 Jul 2013
Article Copyright 2013 by Dholakiya Ankit
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid