Click here to Skip to main content
15,887,952 members
Please Sign up or sign in to vote.
2.37/5 (5 votes)
See more:
I have a gridview in ASP.NET 3.5 and Auto-Generated Filed is enabled and I want to sort data by clicking the grid header. how can I do that? I searched internet but most of it work with the columns from html but mine don't have since they are auto generated. Thanks
Posted
Updated 14-Jun-17 10:18am

For sorting you have to bind the grid...

C#
private void LoadGrid(string sortExpr, string sortDirection)
{


        DataSet ds = new DataSet();
        SqlDataAdapter SqlDA = new SqlDataAdapter("Select * from tablename order by "+sortExpr+"  "+sortDirection+"", connectionString);
        SqlDA.Fill(ds);
        if (ds != null)
        {
            gdManageForm.DataSource = ds;
    gdManageForm.EditIndex = -1;
    gdManageForm.DataBind();
        }

}

protected void gdManageForm_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;
    ViewState["SortExpression"] = sortExpression;
    if (GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView(sortExpression, DESCENDING);
    }
    else
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView(sortExpression, ASCENDING);
    }
}

private void SortGridView(string sortExpression, string direction)
{
    //If it is default state
    LoadGrid(sortExpression, direction);
}

private SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}
If you have any doubts let me know
 
Share this answer
 
v3
Comments
Member 8317792 20-Feb-12 6:04am    
How? When?
sdasasd 9-Aug-13 8:42am    
how to when i click the gridview header like user id the table data will be sorted
Jeevitha royapathi 20-Feb-12 6:20am    
private void LoadGrid(string sortExpr, string sortDirection)
{


DataSet ds = new DataSet();
SqlDataAdapter SqlDA = new SqlDataAdapter("Select * from tablename order by "+sortExpr+" "+sortDirection+"", connectionString);
SqlDA.Fill(ds);
if (ds != null)
{
gdManageForm.DataSource = ds;
gdManageForm.EditIndex = -1;
gdManageForm.DataBind();
}

}

protected void gdManageForm_Sorting(object sender, GridViewSortEventArgs e)
{
pnlform.Controls.Clear();
divformName.Style.Add("display", "none");
lblDispError.Text = "";
litFormName.Text = "";
string sortExpression = e.SortExpression;
ViewState["SortExpression"] = sortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}

private void SortGridView(string sortExpression, string direction)
{
//If it is default state
LoadGrid(sortExpression, direction);
}

private SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}

Here gdManageForm is the GirdName
Member 11822399 8-Jul-15 8:06am    
this solution doesn't work on header click !
static SortDirection GridViewSortDirection;
 protected void chequegrid_Sorting(object sender, GridViewSortEventArgs e)
   {
       string sortExpression = e.SortExpression;
       ViewState["SortExpression"] = sortExpression;
       if (GridViewSortDirection == SortDirection.Ascending)
       {
           GridViewSortDirection = SortDirection.Descending;
           SortGridView(sortExpression, "DESCENDING");
       }
       else
       {
           GridViewSortDirection = SortDirection.Ascending;
           SortGridView(sortExpression, "ASCENDING");
       }
   }

   public void SortGridView(string sortExpression, string direction)
   {
       if (sortExpression == "ss")
       {
           string s;
           if(direction =="DESCENDING")
           {
               s = "order by dbo.registration.id desc";
           }
           else
           {
               s = "order by dbo.registration.id";
           }

           bindbysort(s);
       }
   }

   public void bindbysort(string expre)
   {
       DataSet ds = new DataSet();
       ds = ck.bindgrid(expre);
       if (ds.Tables[0].Rows.Count > 0)
       {
           chequegrid.DataSource = ds;
           Label1.Text = ds.Tables[0].Rows.Count.ToString() + " Result Found";
           chequegrid.DataBind();
       }
       else
       {
           chequegrid.DataSource = null;
           chequegrid.DataBind();

       }
   }


aspx


C#
<asp:gridview id="chequegrid" runat="server" autogeneratecolumns="False" allowsorting="true" xmlns:asp="#unknown">
    CellPadding="4" ForeColor="#333333" GridLines="None" EmptyDataText="There is no any cheque generated yet for this time duration."
     CssClass="table-bordered table g" Width="100%" onsorting="chequegrid_Sorting">
    <alternatingrowstyle backcolor="White" forecolor="#284775" />
    <columns>
       <asp:templatefield headertext="Customer Id" sortexpression="ss">
            
            <itemtemplate>
                <asp:label id="Label1" runat="server">
                    Text='<%#  Eval("customer_reg_id") %>'></asp:label>
            </itemtemplate>
        </asp:templatefield>
         <asp:templatefield headertext="Customer Name">
            
            <itemtemplate>
                <asp:label id="Label1" runat="server">
                    Text='<%#  Eval("name") %>'></asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Date From">
            
            <itemtemplate>
                <asp:label id="Label1" runat="server" text="<%#  Convert.ToDateTime(Eval("date_from")).ToString("dd MMMM, yyyy") %>"></asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Date To">
            
            <itemtemplate>
                <asp:label id="Label2" runat="server" text="<%# Convert.ToDateTime(Eval("date_to")).ToString("dd MMMM, yyyy") %>"> </asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Cheque Amount">
            
            <itemtemplate>
                <asp:label id="Label3" runat="server" text="<%# Eval("income") +"/-" %>"></asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Generated On">
            
            <itemtemplate>
                <asp:label id="Label4" runat="server" text="<%# Convert.ToDateTime(Eval("date_of_clear")).ToString("dd MMMM, yyyy") %>"></asp:label>
            </itemtemplate>
        </asp:templatefield>
       
       
      
       
    </columns>
 

</asp:gridview>
 
Share this answer
 
Comments
Nelek 30-Aug-13 7:38am    
Do you realize the question is 18 months old?
[no name] 30-Aug-13 8:58am    
ooo :P
u.namasivayam 29-Oct-14 2:21am    
DataSet ds = new DataSet();
ds = ck.bindgrid(expre);

What is mean by CK????

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