Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
How to Sort a single Column in Asp.net Gridview
================================================
 
Dear Frnds,
 
am working on asp.net Gridview I have a number of columns in Gridview. I need to sort only the Login time Column. when users Clicks on Header of Gridview Login time column.
 
it should sort that all particular column
 
Please give me good Solution. Thanks
Posted 28-Aug-12 21:36pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi,
 
try this
 
 <asp:gridview xmlns:asp="#unknown">
              ID="grdCause"
              runat="server
              EnableSortingAndPagingCallback="True"
              AllowPaging="True"
              AllowSorting="True"
              onpageindexchanging="grdCause_PageIndexChanging"
              onsorting="grdCause_Sorting"
         >
</asp:gridview>
 
The following method is for Loading the Gridview .In this method am taking dataset and bind that dataset to Gridview.
ExecuteProcedure is the method in my sqlhelper class.For that method Click here.
private void LoadGrid()
    {
       
        DataSet ds = dal.ExecuteProcudere("CauseSelectAll", ht);
        grdCause.DataSource = ds.Tables[0];
        grdCause.DataBind();
    }
 
Gridview pageindexChanging Event  for paging:
protected void grdCause_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCause.PageIndex = e.NewPageIndex;
        LoadGrid();
    }
 
Gridview Sorting Event for Sorting:
 
 protected void grdCause_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
 
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }
 

    }
Use the following method for Sorting:
private void SortGridView(string sortExpression, string direction)
    {
        //  You can cache the DataTable for improving performance
        LoadGrid();
        DataTable dt = grdCause.DataSource as DataTable;
        DataView dv = new DataView(dt);
        dv.Sort = sortExpression + direction;
 
        grdCause.DataSource = dv;
        grdCause.DataBind();
 
    }
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
 
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
 
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value;
 }
    }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

One of the easiest way I found :-
 
before you bind gridview, write following code:-
DataSet ds = ReportColumnBAL.GetGridDetails(Details);
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            if (ViewState["SortExpr"] != null)
                dv.Sort = (string)ViewState["SortExpr"] + " " + (string)ViewState["SortDir"];
            gridview.DataSource = dv;
            gridview.DataBind();
 
ON SORTING EVENT call:-
 
protected void gridview_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["SortExpr"] = e.SortExpression;
            if (ViewState["SortDir"] != null)
                e.SortDirection = (string)ViewState["SortDir"] == "ASC" ? SortDirection.Descending : SortDirection.Ascending;
            ViewState["SortDir"] = e.SortDirection == SortDirection.Ascending ? "ASC" : "DESC";
            // call for function that binds grid again after sorting.
        }
 

In your ASPX page:-
OnSorting="gridview_Sorting"
(in gridview tag)
 
<asp:TemplateField AccessibleHeaderText="Login Time"
                                        HeaderText="Login Time" SortExpression="logintime">
                                        
                                        <ItemTemplate>
                                            <asp:Label ID="Label1" runat="server" Text='<%#Bind("logintime")%>'></asp:Label>
                                        </ItemTemplate>
 
</asp:TemplateField>
 

Hope that helps Smile | :)
  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 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 Feb 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