Positive/negative bidirectional sorting of DataGrid

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.