Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys!

I have read lots of articles in the net regarding my question. I would like to sort my records in descending order whenever I click the Header. But I am still wondering on some other terms and sometimes I do not understand in the articles that I found. Can anyone help me out? Thanks in advance!
Posted
Comments
Kornfeld Eliyahu Peter 12-Aug-14 7:12am    
Read here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.allowsorting(v=vs.110).aspx
Wombaticus 12-Aug-14 7:17am    
also try 4guysfromrolla
http://aspnet.4guysfromrolla.com/default.aspx
either browse or search for what you want. They have tons of good learning articles there.

Dear DarkDreamer08,


Please, following step to enable your gridview for sorting enable.

1. Select GRIDVIEW and go to property window select AllowSorting make it True

above very general kind of enabling sorting of gridview.

After that run your application check it.

If directly attached SQLDatasource then its ok, other kind of datasource then you have to check it for fruther treatment.

For fruther thing show me your code.
Manoj Kalla.
 
Share this answer
 
//------ DATAVIEW FOR SORTING MILESTONE
private DataView Getdata()
{
connection();
amicassaCon.Open();
DataSet dsMilestone = new DataSet();
string strSelectCmd = "SELECT * FROM milestone ";
SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, amicassaCon);
da.Fill(dsMilestone, "milestone");
DataView dvMilestone = dsMilestone.Tables["milestone"].DefaultView;
dvMilestone.Sort = ViewState["SortExpr"].ToString();
return dvMilestone;

}

CODE AT PAGE LOAD

ViewState["SortExpr"] = Sort_Direction;
DataView dvMiles = Getdata();
GridView_milestone.DataSource = dvMiles;
GridView_milestone.DataBind();


C#
//---------------SORTING MILESTONE
   protected void Sorting(object sender, GridViewSortEventArgs e)
   {
       string[] SortOrder = ViewState["SortExpr"].ToString().Split(' ');
       if (SortOrder[0] == e.SortExpression)
       {
           if (SortOrder[1] == "ASC")
           {
               ViewState["SortExpr"] = e.SortExpression + " " + "DESC";
           }
           else
           {
               ViewState["SortExpr"] = e.SortExpression + " " + "ASC";
           }
       }
       else
       {
           ViewState["SortExpr"] = e.SortExpression + " " + "ASC";
       }
       GridView_milestone.DataSource = Getdata();
       GridView_milestone.DataBind();
   }


Then allow sorting to the grid view and include the SortExpression="FIELDNAME" to each header :)
 
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