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

Sort gridview without viewstate [source code]

Rate me:
Please Sign up or sign in to vote.
3.80/5 (11 votes)
13 Aug 2013CPOL 84.2K   425   28  
Sorting GridView using attribute property of GridView
This is an old version of the currently published tip/trick.

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.NET
<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 its working >> 

C#
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

I am sure this will definitely help you. if you still not get it see source code... If you like it rate it. 

License

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

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.