Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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

hi,

try this

ASP.NET
 <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.
C#
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:

C#
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:
C#
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;
 }
    }
 
Share this answer
 
v2
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:-

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

XML
<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 :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900