The cross-list header design of the DataGrid in Asp.net!

Source: Internet
Author: User

1. DataGrid attribute settings
1. allowpaging: True
2. pagestyle-> position: topandbottom
3. Optional: pagestyle-> horizonalign: Center (center text)
4. Optional: itemstyle-> horizonalign: Center (center text)
II,CodePart
1. First bind the DataGrid to a table in the database, for example:
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! Ispostback)
{
Sqlconnection myconn = new sqlconnection ("Server = localhost; uid = sa; Pwd = sa; database = db_test ");
Sqldataadapter da = new sqldataadapter ("select * from individual", myconn );
Dataset DS = new dataset ();
Da. Fill (DS, "GR ");
Dggeren. datasource = Ds. Tables ["GR"]. defaultview;
Dggeren. databind ();

}
2. Add the itemcreated event handler for the DataGrid,
3. To determine the two (up and down) pager locations in the DataGrid, we can use a global variable to determine.
Define a global variable private int m_createpagetimes = 0;
4. add content to the processing function of the itemcreated event of the DataGrid as follows:
Private void dggeren_itemcreated (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
Switch (E. Item. itemtype)
{
// Case (listitemtype. pager ):
Case listitemtype. Pager:
{
If (m_createpagetimes = 0)
{
Datagriditem ROW = (datagriditem) E. item;
Row. cells. Clear ();

// Row. backcolor = color. Navy; // background color
// Row. forecolor = color. Red; // foreground color

Row. horizontalalign = horizontalalign. Center; // center the text

Tablecell cell0 = new tablecell ();
Cell0.rowspan = 2;
Cell0.controls. Add (New literalcontrol ("name "));

Tablecell cell1 = new tablecell ();
Cell1.columnspan = 2; // The default columnspan value is 1.
Cell1.text = "housing address information ";

// You can also do this: cell1.controls. Add (New literalcontrol ("housing address information "));

// Tablecell cell2 = new tablecell ();
// Cell2.controls. Add (New literalcontrol (""));
Tablecell cell2 = new tablecell ();
Cell2.rowspan = 2;
Cell2.text = "Date of Birth ";

Row. cells. Add (cell0 );
Row. cells. Add (cell1 );
Row. cells. Add (cell2 );

M_createpagetimes ++;
}
Break;
}
Case listitemtype. header:
{
Datagriditem head = (datagriditem) E. item;
Head. cells. Clear ();

// Head. verticalalign = verticalalign. Middle;
// Head. horizontalalign = horizontalalign. Center;

// Tablecell cell00 = new tablecell ();
// Cell00.rowspan = 2;
// Cell00.text = "name ";

Tablecell cell01 = new tablecell ();
Cell01.text = "Building No ";

Tablecell cell02 = new tablecell ();
Cell02.text = "room number ";

// Tablecell cell03 = new tablecell ();
// Cell03.text = "Date of Birth ";

// Head. cells. Add (cell00 );
Head. cells. Add (cell01 );
Head. cells. Add (cell02 );
// Head. cells. Add (cell03 );
Break;
}

 

}

}

3. The final effect is as follows:


4. Hope you can give me some advice!

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.