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

Tagged as

Gridview Sorting using attribute [Source Code]

, 30 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Sorting GridView using attribute property of GridView

Introduction

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" 
        onprerender="GridView1_PreRender" 
         PageSize="2">
      <Columns>
        <asp:BoundField  DataField="EmployeeId" HeaderText="Last Name"  
              ItemStyle-Width="15%" SortExpression="EmployeeId" >
        <ItemStyle Width="15%"></ItemStyle>
          </asp:BoundField>
        <asp:BoundField DataField="Name" 
        HeaderText="First Name"  ItemStyle-Width="15%" 
              SortExpression="Name"  >
             <ItemStyle Width="15%"></ItemStyle>
          </asp:BoundField>
        <asp:BoundField DataField="gender" 
        HeaderText="Email" ItemStyle-Width="15%" >
            <ItemStyle Width="15%"></ItemStyle>
          </asp:BoundField>
        <asp:TemplateField HeaderText="Change Password"  
        ItemStyle-Width="15%">

        <ItemTemplate>

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

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

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;
            }
            else
            {
                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.

License

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

Share

About the Author

AJD-salute
Web Developer
India India
No Biography provided

Comments and Discussions

 
AnswerRe: demo PinprofessionalMaimonides13-Aug-13 22:04 
GeneralRe: demo Pinmemberdholakiya ankit13-Aug-13 22:16 
QuestionMy Vote of 3 PinmemberSruthiR13-Aug-13 18:34 
AnswerRe: My Vote of 3 Pinmemberdholakiya ankit13-Aug-13 18:36 
AnswerRe: My Vote of 3 Pinmemberdholakiya ankit13-Aug-13 23:07 
GeneralRe: My Vote of 3 PinmemberSruthiR14-Aug-13 0:55 
GeneralRe: My Vote of 3 Pinmemberdholakiya ankit14-Aug-13 1:20 
GeneralMy vote of 3 PinprofessionalAlexCode6-Aug-13 20:56 
GeneralRe: My vote of 3 Pinmemberdholakiya ankit6-Aug-13 21:04 
GeneralMy vote of 1 PinmemberMehedi Hassan Mody5-Aug-13 22:32 
GeneralRe: My vote of 1 Pinmemberdholakiya ankit5-Aug-13 22:46 
GeneralMy vote of 3 PinmemberHaBiX4-Aug-13 3:45 
GeneralRe: My vote of 3 Pinprofessionaldholakiya ankit4-Aug-13 18:12 
GeneralRe: My vote of 3 Pinmemberprakash.padariya20-Jan-14 19:18 
GeneralRe: My vote of 3 PinmemberAJD-salute3-Feb-14 15:42 
GeneralMy vote of 5 PinmemberWeerayut Teja3-Aug-13 17:35 
GeneralRe: My vote of 5 Pinprofessionaldholakiya ankit4-Aug-13 18:11 
GeneralMy vote of 3 Pinmemberaashi272-Aug-13 0:44 
GeneralRe: My vote of 3 [modified] Pinprofessionaldholakiya ankit2-Aug-13 0:50 
QuestionPlease feedfack about this Pinprofessionaldholakiya ankit2-Aug-13 0:11 

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
Web04 | 2.8.141015.1 | Last Updated 31 Aug 2013
Article Copyright 2013 by AJD-salute
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid