Practical tips: Two-way sequencing of the DataGrid

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

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.