Mikecat)
From: old cat ghost ideal
Haha, I did not writeArticleMeaning, see friendsHbzxf)I decided to write some of my usual articles to share with you! For any omissions, please kindly advise!
In Asp.net, it is convenient to use the DataGrid Control to sort by column. However, we can only sort individual items! If we need positive and negative sorting, we need to add someCodeLet's take a look at this process in detail.
First, we need to set the attribute of the initird control to allowsorting = "true", and the sorting expression eg: sortexpression = "kmdm" needs to be formulated for the sorting column ". After these settings are complete, we enter the code file to compile the events in response to the sorting.
Add the following code to the page_load time:
If (! Ispostback)
{< br>
If (this. kjkm_dg.attributes ["sortexpression"] = NULL) // here, kjkm_dg is the DataGrid id
{< br>
This. kjkm_dg.attributes ["sortexpression"] = "kmdm"; // Add a sorting attribute to the DataGrid, and the default sorting expression is kmdm;
kjkm_dg.attributes ["sortdirection"] = "ASC"; // Add a sorting direction attribute to the DataGrid, Which is sorted in ascending order by default;
}< br>
mikecatbind (); // bind a function. 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; // dataset from the web service. You can use a DS file here;
DV. Sort = sortexpression + "" + sortdirection; // specify the sorting method of the view;
Kjkm_dg.datasource = DV; // specify the data source
Kjkm_dg.databind (); // Data Binding
}
After completing the above settings, we will enter an important step to write sorting events:
Private void kjkm_dg_sortcommand (Object source, system. Web. UI. webcontrols. datagridsortcommandeventargs E)
{
String sortexpression = E. sortexpression. tostring (); // obtain the current sort expression
String sortdirection = "ASC"; // assigns an initial value to the sort direction variable
If (sortexpression = kjkm_dg.attributes ["sortexpression"]) // if it is the current sorting Column
{
Sortdirection = (kjkm_dg.attributes ["sortdirection"]. tostring () = sortdirection? "DESC": "ASC"); // obtain the next sorting status
}
Kjkm_dg.attributes ["sortexpression"] = sortexpression;
Kjkm_dg.attributes ["sortdirection"] = sortdirection;
Mikecatbind ();
}
Okay. Try to see if it can be sorted in reverse order.