first, the basic work to achieve this function.
1, first add a GridView, named Gvdata.
2, set the properties of the control: the steps are as follows.
Set properties:
These 4 properties, and you want to set the control allowsorting= "True", 5 points are indispensable.
3, write the control of the Gvdata_sorting event
Double-click, go in to write. second, code implementation
Usingsystem.data;
UsingSystem.Data.SqlClient; Namespace sort {Public partial class WebForm2:System.Web.UI.Page {protected Voidpage_load (object sender, Ev Entargs e) {if (!
IsPostBack) {//Give it a default state first, because the following bin () method requires a state value.
viewstate["SortOrder"] = "depid";
viewstate["Orderdire"] = "ASC";
Bind ();
Click the DataGrid header field to trigger the event, pass the DataField value of the corresponding field, and reset the viewstate["SortOrder" and viewstate["Orderdire" properties. Protected voidgvdata_sorting (Objectsender, GridViewSortEventArgs e) {stringspage = E.sortexpressi
On if (viewstate["SortOrder"]. ToString () = = Spage) {if (viewstate["Orderdire"].
ToString () = = "Desc") viewstate["orderdire"] = "ASC";
else viewstate["orderdire"] = "Desc";
} else { viewstate["SortOrder"] =e.sortexpression;
Bind ();
//Get the data, sort by customizing the view, and reorder. Public Voidbind () {SqlConnection con= New SqlConnection ("server=.;
database=department;uid= ' sa ';p wd= ' 123456 '); Con.
Open ();
String str= "select * from Tdepartment";
SqlCommand cmd= New SqlCommand (Str,con);
Sqldataadaptersqlda = new SqlDataAdapter (cmd);
Datatabletabdata = new DataTable ();
Sqlda.fill (Tabdata);
Sort by customizing the view Dataviewview = Tabdata.defaultview;
Stringsort = (string) viewstate["SortOrder"] + "" + (String) viewstate["Orderdire"]; View.
sort = sort;
Gvdata.datasource = view;
Gvdata.databind (); Con.
Close (); }
}
}
Three, the realization clicks the table header, realizes the rearrangement, the effect is as follows:
Iv. Summary
It's hard to feel when you're not getting to know what needs are, and it's not that hard when it comes to getting to know the needs, it's really not that hard when it really comes true. Difficult things, always want to escape the difficult heart. Many facts prove: As long as you believe in yourself, calm face difficulties, many problems will no longer be a problem. So...