Sorting method of ASP. NET gridview point question mark

Source: Internet
Author: User
Today, we need to sort the vertices in the gridview. After a long time, we finally succeeded. Before viewing the Token, you need to explain the following:
The gridview itself has a sort () function: Public Virtual void sort (string sortexpression, sortdirection) which is quite convenient to use. However, in actual use, it is often used to sort the titles of each column. clicking the title triggers the protected void gridview_sorting (Object sender, gridviewsorteventargs e) event. Sortdirection and sortexpression can be obtained from gridviewsorteventargs. However, the gridview. Sort () itself also triggers gridview_sorting (). If you call the gridview. Sort () method in gridview_sorting (), the loop is infinite until the stack overflows. In this example, the dataview. Sort attribute is used for sorting. Sorting event ), And then reset it to the gridview.

The token generation is as follows:

1. Add allowsorting annotation to the gridview in the source topology. // 1. Add allowsorting = "true" to the Source Vertex's gridview"
< ASP: gridview ID = " Gvdata " Runat = " Server " Allowsorting = " True " >
</ ASP: gridview >

2. clbcomunication. clscomunication defines a property that is used to store the information obtained from the data volume:
Note: It is assumed that the metadata of the metadata table (mdatatable) obtained by the dataset is cleared after the dataset is displayed. Therefore, the expected information will be stored in the category for reuse.

///   <Summary>
/// Changes in dataview adequacy
///   </Summary>
Private   Static Dataview dvvalue =   New Dataview ();
///   <Summary>
/// Data Retention
///   </Summary>
Public   Static Dataview dv
{
Get { Return Dvvalue ;}
Set {Dvvalue = Value ;}
}

3. store the data in the dataview category.

// Get the data (bofactory. bofactory. getoracledatatable (msql_selection) to get the data from the data Preview)
Datatable mdatatable = Bofactory. bofactory. getoracledatatable (msql_selection );
// Put the information to be sorted into dataview (this sentence is mainly used)
Clbcomunication. clscomunication. DV =   New Dataview (mdatatable );
// Information and page-based gridview Tuning
Gvdata. databind ();

4. Sort rows gridview's sorting event

// The sorting event of the gridview
Gvdata. Sorting + =   New Gridviewsorteventhandler (gvdata_sorting );

5. Optimize the sorting event method of the gridview. The definition of gridviewsortction ction semantics used is shown in "6 ".

///   <Summary>
/// The sorting event method of the gridview
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Void Gvdata_sorting ( Object Sender, gridviewsorteventargs E)
{
String Sortexpression = E. sortexpression. toupper ();
If (Gridviewsortction ction = Sortdirection. ascending)
{
Gridviewsortction ction = Sortdirection. Descending;
// Sort and reset
Binddata (sortexpression, " Desc " );
}
Else   If (Gridviewsortction ction = Sortdirection. Descending)
{
Gridviewsortction ction = Sortdirection. ascending;
// Sort and reset
Binddata (sortexpression, " ASC " );
}
}

6. define the direction of sorting. This attribute uses viewstate to save the direction of each sorting:

///   <Summary>
/// Sort direction
///   </Summary>
Public Sortdirection gridviewsortction ction
{
Get
{
If (Viewstate [ " Sortdirection " ] =   Null )
Viewstate [ " Sortdirection " ] = Sortdirection. ascending;
Return (Sortdirection) viewstate [ " Sortdirection " ];
}
Set
{
Viewstate [ " Sortdirection " ] = Value;
}
}

7. sort and sort data

///   <Summary>
/// Sort and optimize data
///   </Summary>
///   <Param name = "sortexpression"> </param>
///   <Param name = "sortdirection"> </param>
Protected   Void Binddata ( String Sortexpression, String Sortdirection)
{
Clbcomunication. clscomunication. DV. Sort = Sortexpression;
If (Sortdirection ! = String. Empty)
{
Clbcomunication. clscomunication. DV. Sort = Sortexpression + "   "   + Sortdirection;
}
Gvdata. datasource = Clbcomunication. clscomunication. DV;
Gvdata. databind ();
}

Other quizzes:

Http://www.cnblogs.com/jackyrong/archive/2006/05/26/409788.html

Http://stlh.blogspot.com/2008/02/gridview-sort.html

Related Article

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.