Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I am trying to sort a column 'mem_id 'in already generated gridview. I dont get the result. Hereunder are my codes:
 
<asp:GridView ID="GridView2" runat="server"  HorizontalAlign="Center"  AutoGenerateColumns="False" allowpaging="True"  AllowSorting="True"
OnSorting="GridView2_Sorting" PageSize="12" onpageindexchanging="GridView2_PageIndexChanging" onselectedindexchanged="GridView2_SelectedIndexChanged"
  style="border-color: #808000; font-size:small; top: 176px; left: 27px; position: absolute; height: 75px; width:1350px" >
 

            <RowStyle HorizontalAlign="Center" />
            <Columns >
            <asp:BoundField DataField="mem_id" HeaderText="Mem_ID" sortexpression="Mem_id"/>
            <asp:BoundField DataField="mem_name" HeaderText="Mem Name"/>
            <asp:BoundField DataField="mobile" HeaderText="Cell No:"/>
 

code behind:
 
protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
GridView Gridview2 = (GridView)sender;
SqlDataSource ds = (SqlDataSource)Gridview2.DataSource;
ds.SelectCommand = ds.SelectCommand + " order by "
+ e.SortExpression;
Gridview2.DataSource = ds;
Gridview2.DataBind();
 
Can anyone guide me?
Posted 11-Jul-13 5:45am
Edited 11-Jul-13 5:51am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Create a sort property for the Sorting type
public SortDirection sortProperty
    {
        get
        {
            if (ViewState["SortingState"] == null)
            {
                ViewState["SortingState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["SortingState"];
        }
        set
        {
            ViewState["SortingState"] = value;
        }
    }

 
and inside the Sorting method
 

 dsProjects = projectDetails.GetProjectHistory(UserName);
        if (sortProperty == SortDirection.Ascending)
        {
            sortProperty = SortDirection.Descending;
            sortingDirection = "Desc";
        }
        else
        {
            sortProperty = SortDirection.Ascending;
            sortingDirection = "Asc";
             }
        DataView sortedView = new DataView(dsProjects.Tables[0]);
        sortedView.Sort = e.SortExpression + " " + sortingDirection;
        gvProjectHistory.DataSource = sortedView;
        gvProjectHistory.DataBind();
 
also check this links, it may helps you
 
http://csharpdotnetfreak.blogspot.com/2012/06/sorting-gridview-columns-headers-aspnet.html[^]
 
http://asp-net-example.blogspot.in/2008/12/aspnet-gridview-sorting-example-how-to.html[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

this is very old method of viewstate i have new idea do this
 
gridview.attributes.add("lastsort",'asc');
 
when user click on row then get value of attribute that is lastsort and its value is asc
 
and when clicked change attribute style to
gridview.attributes.add("lastsort",'desc');
so second time you get desc and vice versa its working excellent for me.....
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 BillWoodruff 159
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 11 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100