datagrid| Skills | Sort Oh, this did not write the meaning of the article, see Friends Ahau (HBZXF) of the article, also decided to put some of the usual things to write out and share! There are omissions in the place, please advise!
It is convenient to use the DataGrid control to sort by column in asp.net. But we can only single order! If we need to have a positive and negative sort then we need to add some code control, let me explain the process in detail.
First we need to set the properties of the Datagird control to allowsorting= "True", and we need to arrange the sort expression eg:sortexpression= "kmdm". Setting these up, we go into code files to write events that respond to sorting.
First, add the following code to the Page_Load time:
if (! IsPostBack) { if (THIS.KJKM_DG). attributes["SortExpression"]==null)//Here KJKM_DG for DataGrid ID { this.kjkm_dg. attributes["SortExpression"]= "KMDM"; This adds a sort attribute to the DataGrid, and the default sort expression is kmdm; KJKM_DG. attributes["SortDirection"]= "ASC"; This adds a sort direction attribute to the DataGrid and is sorted by default in ascending order; } Mikecatbind (); Binding functions, the following describes } protected void Mikecatbind () { string sqlstr= "select * from zc_kjkm"; DataView dv=new DataView (); String SORTEXPRESSION=KJKM_DG. attributes["SortExpression"]; String SORTDIRECTION=KJKM_DG. attributes["SortDirection"]; Dv=us. Bind (SQLSTR). Tables[0]. DefaultView; A dataset from the Web service, where a DS is possible; Dv. sort=sortexpression+ "" +sortdirection; Specifies how the view is sorted; KJKM_DG. DATASOURCE=DV; Specifies the data source KJKM_DG. DataBind (); Data Binding }
|
After the above setup we go into the important part of the sequencing event:
private void Kjkm_dg_sortcommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { string sortexpression=e.sortexpression.tostring (); Gets the current sort expression string sortdirection= "ASC";//assigns an initial value if (SORTEXPRESSION==KJKM_DG) to the sort direction variable . Attributes ["SortExpression"]) //If the current row sequence { sortdirection= (KJKM_DG. attributes["SortDirection"]. ToString () ==sortdirection? " DESC ":" ASC "); Gets the next sort state } KJKM_DG. attributes["SortExpression"]=sortexpression; KJKM_DG. attributes["SortDirection"]=sortdirection; Mikecatbind (); }
|
OK, let's try to see if it can be done in reverse order.