[ASP. NET + C #] DataGrid Tip 2 (how to sort dynamically created columns)

Source: Internet
Author: User

This is a problem I have solved in my recent work.
If there is a better way for a friend, please send it to me. Thank you, younger brother.
I. dynamically create column Functions

Private void datagriddatabaind (){
Int I;
String strtempvalue;
Datatable dtcom = (datatable) session ["Source"];
Datatable dtsub = (datatable) session ["subsource"];
Datagrid1.allowsorting = true;
Strtempvalue = convert. tostring (session ["strtempvalue"]). Trim ();
For (I = 0; I <dtsub. Rows. Count; I ++)
{
If (dtsub. Rows [I] [1]. tostring (). Trim (). Equals ("Y "))
{
Boundcolumn DGCA = new boundcolumn ();
DGCA. headertext = "a volume," + dtsub. Rows [I] [0]. tostring ();
DGCA. itemstyle. horizontalalign = horizontalalign. Center;
DGCA. datafield = "S" + convert. tostring (I + 1) + "";
DGCA. headerstyle. horizontalalign = horizontalalign. Center;
DGCA. headerstyle. width = unit. pixel (60 );
Datagrid1.columns. Add (DGCA );

Boundcolumn dgcb = new boundcolumn ();
Dgcb. headertext = "B volume ";
Dgcb. itemstyle. horizontalalign = horizontalalign. Center;
Dgcb. datafield = "S" + convert. tostring (I + 1) + "B ";
Dgcb. headerstyle. horizontalalign = horizontalalign. Center;
Dgcb. headerstyle. width = unit. pixel (60 );
Datagrid1.columns. Add (dgcb );

Boundcolumn DGC = new boundcolumn ();
DGC. headertext = "Comprehensive ";
DGC. itemstyle. horizontalalign = horizontalalign. Center;
DGC. datafield = "S" + convert. tostring (I + 1 );
DGC. headerstyle. horizontalalign = horizontalalign. Center;
DGC. headerstyle. width = unit. pixel (60 );;
Datagrid1.columns. Add (DGC );
}
Else
{
Boundcolumn DGC = new boundcolumn ();
DGC. headertext = dtsub. Rows [I] [0]. tostring ();
DGC. itemstyle. horizontalalign = horizontalalign. Center;
DGC. datafield = "S" + convert. tostring (I + 1 );
DGC. headerstyle. horizontalalign = horizontalalign. Center;
DGC. headerstyle. width = unit. pixel (60 );
Datagrid1.columns. Add (DGC );
}
}
Boundcolumn dgc2 = new boundcolumn ();
Dgc2.headertext = "total score ";
Dgc2.itemstyle. horizontalalign = horizontalalign. Center;
Dgc2.datafield = "fullscore ";
Dgc2.sortexpression = "fullscore DESC ";
Dgc2.headerstyle. horizontalalign = horizontalalign. Center;
Dgc2.headerstyle. width = unit. pixel (60 );
Datagrid1.columns. Add (dgc2 );

Boundcolumn dgc1 = new boundcolumn ();
Dgc1.headertext = "Class ranking ";
Dgc1.itemstyle. horizontalalign = horizontalalign. Center;
Dgc1.datafield = "orders ";
Dgc1.sortexpression = "orders ";
Dgc1.headerstyle. horizontalalign = horizontalalign. Center;
Dgc1.headerstyle. width = unit. pixel (60 );
Datagrid1.columns. Add (dgc1 );
If (strtempvalue. Trim (). Equals (""))
{
Boundcolumn dgcgrade = new boundcolumn ();
Dgcgrade. headertext = "grade ranking ";
Dgcgrade. itemstyle. horizontalalign = horizontalalign. Center;
Dgcgrade. datafield = "gradeorders ";
Dgcgrade. sortexpression = "gradeorders ";
Dgcgrade. headerstyle. horizontalalign = horizontalalign. Center;
Dgcgrade. headerstyle. width = unit. pixel (60 );
Datagrid1.columns. Add (dgcgrade );
}

}

Ii. Data Binding Functions
Private void datagriddatabaind1 (){
Datatable dtcom = (datatable) session ["Source"];
Datagrid1.datasource = dtcom. defaultview;
Datagrid1.databind ();
}

3. events to be called
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid itemcreated );
This. datagrid1.sortcommand + = new system. Web. UI. webcontrols. datagridsortcommandeventhandler (this. datagrid1_sortcommand );
This. Load + = new system. eventhandler (this. page_load );
This. init + = new system. eventhandler (this. scoreorderwebexe_init );
Iv. Sorting
Private void datagrid1_sortcommand (Object source, system. Web. UI. webcontrols. datagridsortcommandeventargs E)
{

Datatable dtcom = (datatable) session ["Source"];
Dtcom. defaultview. Sort = E. sortexpression;
Session ["Source"] = dtcom;
Datagriddatabaind1 ();
}
5. Load dynamically generated Columns
Private void scoreorderwebexe_init (Object sender, system. eventargs E)
{
If (ispostback ){
Datagriddatabaind ();
}
}

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.