Implementation of complex headers in the gridview infinite Layer in ASP. NET 2.0)

Source: Internet
Author: User

1.

C #

<% @ Page Language = "C #" autoeventwireup = "true" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

for (INT I = 0; I <8; I ++)
{< br> system. random RD = new system. random (environment. tickcount * I);
DR = DT. newrow ();
Dr [0] = "class" + I. tostring ();
Dr [1] = "student" + I. tostring ();
Dr [2] = system. math. round (Rd. nextdouble () * 100, 2);
Dr [3] = system. math. round (Rd. nextdouble () * 100, 2);
Dr [4] = system. math. round (Rd. nextdouble () * 100, 2);
Dr [5] = system. math. round (Rd. nextdouble () * 100, 2);
DT. rows. add (DR);
}< br> system. data. dataview DV = new system. data. dataview (DT);
return DV;
}

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Gridview1.bordercolor = system. Drawing. color. darkorange;
Gridview1.datasource = createdatasource ();
Gridview1.databind ();
}
}

Protected void gridview1_rowcreated (Object sender, gridviewroweventargs E)
{

If (E. Row. rowtype = datacontrolrowtype. header)
{
// Create a gridviewrow, which is equivalent to the tr row of the table
Gridviewrow rowheader = new gridviewrow (0, 0, datacontrolrowtype. header, datacontrolrowstate. Normal );
String headerbackcolor = "# ededed ";
Rowheader. backcolor = system. Drawing. colortranslator. fromhtml (headerbackcolor );

// Determine the Header style to be displayed. It can also be generated by computation.

// <Tr>
// <TD rowspan = '2'> key cells </TD>
// <TD colspan = '2'> header text </TD>
// <TD colspan = '2'> header text </TD>
// <TD> header text </TD>
// </Tr>
// <Tr bgcolor = '# fff'>
// <TD colspan = '2'> header text </TD>
// <TD rowspan = '2'> header text </TD>
// <TD colspan = '2'> header text </TD>
// </Tr>
// <Tr bgcolor = '# fff'>
// <TD> header text </TD>
// <TD> header text </TD>
// <TD> header text </TD>
// <TD> header text </TD>
// <TD> header text </TD> ";
// </Tr>
// You can set a diagonal line for the above Style

Literal newcells = new literal ();
Newcells. Text = @ "header text 1 </Th>
<TH colspan = '2'> header text 2 </Th>
<TH colspan = '2'> header Text 3 </Th>
<TH> header text 4 </Th>
</Tr>
<Tr bgcolor = '"+ headerbackcolor +"'> ";
Newcells. Text + = @"
<TH colspan = '2'> header text 5 </Th>
<TH rowspan = '2'> header text 6 </Th>
<TH colspan = '2'> header text 7 </Th>
</Tr>
<Tr bgcolor = '"+ headerbackcolor +"'> ";
Newcells. Text + = @"
<TH> header text 8 </Th>
<TH> header text 9 </Th>
<TH> header text 10 </Th>
<TH> header text 11 </Th>
<TH> header text 12 ";

Tablecellcollection cells = E. Row. cells;
Tableheadercell headercell = new tableheadercell ();
// The following attribute settings must be consistent with <TD rowspan = '2'> key cells </TD>.
Headercell. rowspan = 2;
Headercell. Controls. Add (newcells );
Rowheader. cells. Add (headercell );

Rowheader. cells. Add (headercell );
Rowheader. Visible = true;

// Add to gridview1
Gridview1.controls [0]. Controls. addat (0, rowheader );
}
}

protected void gridview1_rowdatabound (Object sender, gridviewroweventargs e)
{< br> If (E. row. rowtype = datacontrolrowtype. header)
{< br> E. row. attributes. add ("style", "Background: # 9999ff; color: # ffffff; font-size: 14px ");
}< br> else
{< br> E. row. attributes. add ("style", "Background: # fff");
}< BR >}< br>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Add a multi-layer header for the gridview </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: gridview id = "gridview1" runat = "server" cellspacing = "1" cellpadding = "3" font-size = "12px"
Width = "600px" backcolor = "#000000" borderwidth = "0" onrowdatabound = "gridview1_rowdatabound"
Onrowcreated = "gridview1_rowcreated">
</ASP: gridview>
</Form>
</Body>
</Html>
VB. NET

<% @ Page Language = "VB" autoeventwireup = "true" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

DR = DT. newrow
DR (0) = "class" + I. tostring
DR (1) = "student" + I. tostring
DR (2) = system. math. round (Rd. nextdouble * 100, 2)
DR (3) = system. math. round (Rd. nextdouble * 100, 2)
DR (4) = system. math. round (Rd. nextdouble * 100, 2)
DR (5) = system. math. round (Rd. nextdouble * 100, 2)
DT. rows. add (DR)
system. math. min (system. threading. interlocked. increment (I), I-1)
end while
dim DV as system. data. dataview = new system. data. dataview (DT)
return DV
end function

Protected sub page_load (byval sender as object, byval e as eventargs)
If not ispostback then
Gridview1.bordercolor = system. Drawing. color. darkorange
Gridview1.datasource = createdatasource ()
Gridview1.databind ()
End if
End sub

Protected sub gridview1_rowcreated (byval sender as object, byval e as gridviewroweventargs)
If E. Row. rowtype = datacontrolrowtype. header then
Dim rowheader as gridviewrow = new gridviewrow (0, 0, datacontrolrowtype. header, datacontrolrowstate. Normal)
Dim headerbackcolor as string = "# ededed"
Rowheader. backcolor = system. Drawing. colortranslator. fromhtml (headerbackcolor)
Dim newcells as literal = new literal
Newcells. Text = "header text 1 </Th>" + _
"<TH colspan = '2'> header text 2 </Th>" + _
"<TH colspan = '2'> header Text 3 </Th>" + _
"<TH> header text 4 </Th>" + _
"</Tr>" + _
"<Tr bgcolor = '" + headerbackcolor + "'>" + _
"<TH colspan = '2'> header text 5 </Th>" + _
"<TH rowspan = '2'> header text 6 </Th>" + _
"<TH colspan = '2'> header text 7 </Th>" + _
"</Tr>" + _
"<Tr bgcolor = '" + headerbackcolor + "'>" + _
"<TH> header text 8 </Th>" + _
"<TH> header text 9 </Th>" + _
"<TH> header text 10 </Th>" + _
"<TH> header text 11 </Th>" + _
"<TH> header text 12"
Dim cells as tablecellcollection = E. Row. Cells
Dim headercell as tableheadercell = new tableheadercell
Headercell. rowspan = 2
Headercell. Controls. Add (newcells)
Rowheader. cells. Add (headercell)
Rowheader. cells. Add (headercell)
Rowheader. Visible = true
Gridview1.controls (0). Controls. addat (0, rowheader)
End if
End sub

protected sub gridview1_rowdatabound (byval sender as object, byval e as gridviewroweventargs)
If e. row. rowtype = datacontrolrowtype. header then
E. row. attributes. add ("style", "Background: # 9999ff; color: # ffffff; font-size: 14px")
else
E. row. attributes. add ("style", "Background: # fff")
end if
end sub

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Add a multi-layer header for the gridview </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: gridview id = "gridview1" runat = "server" cellspacing = "1" cellpadding = "3" font-size = "12px"
Width = "600px" backcolor = "#000000" borderwidth = "0" onrowdatabound = "gridview1_rowdatabound"
Onrowcreated = "gridview1_rowcreated">
</ASP: gridview>
</Form>
</Body>
</Html>

2. bind an event to the DataGrid dataCodeImplementation

Tablecellcollection Tc = E. Item. cells;
If (E. Item. itemtype = listitemtype. header)
{
// TC [0] = new tableheadercell ();
TC [0]. rowspan = 2;
TC [7]. style. Add ("border-left", "1px solid #000 ");
TC. removeat (E. Item. cells. Count-1 );

Tableheadercell thc1 = new tableheadercell ();
Thc1.text = "New to quidsi (First Order contains :)";
Thc1.style. Add ("border-bottom" , "Solid 2px #000 ;");
Thc1.columnspan = 6;
TC. addat (1, thc1 );
Tableheadercell thc2 = new tableheadercell ();
Thc2.text = "existing customer ";
Thc2.style. Add ("border-bottom" , "2px solid #000 ");
Thc2.style. Add ("border-left", "1px solid #000 ");
Thc2.columnspan = 2;
TC. addat (2, thc2 );

tableheadercell thc3 = new tableheadercell ();
thc3.text = "Total ";
thc3.rowspan = 2;
TC. addat (3, thc3);
// TC [5]. TEXT = + Tc [5]. text;
}< br> else
TC [7]. style. add ("border-left", "1px solid #000");

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.