Realization of the complex table Head of the GridView infinite layer in asp.net 2.0

Source: Internet
Author: User
Tags header key net string tostring

The way to do this is to populate the cell with the format code we want.

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" >

<script runat= "Server"
 //calculation data can be obtained from the Data View
  ICollection CreateDataSource ()
  {
    System.Data.DataTable dt = new System.Data.DataTable ();
    System.Data.DataRow Dr;
    dt. Columns.Add (New System.Data.DataColumn ("Student Class", typeof (System.String));
    dt. Columns.Add (New System.Data.DataColumn ("Student Name", typeof (System.String));
    dt. Columns.Add (New System.Data.DataColumn ("language", typeof (System.Decimal));
    dt. Columns.Add (New System.Data.DataColumn ("Mathematics", typeof (System.Decimal));
    dt. Columns.Add (New System.Data.DataColumn ("English", typeof (System.Decimal));
    dt. Columns.Add (New System.Data.DataColumn ("Computer", typeof (System.Decimal));

    for (int i = 0; i < 8; i++)
    {
   & nbsp;  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);
   }
    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)
{
Creates a gridviewrow, equivalent to a TR row of tables
GridViewRow Rowheader = new GridViewRow (0, 0, Datacontrolrowtype.header, datacontrolrowstate.normal);
String headerbackcolor = "#EDEDED";
Rowheader.backcolor = System.Drawing.ColorTranslator.FromHtml (HeaderBackColor);

//implementation determines which header style to display, or it can be generated by calculating

<tr>
&LT;TD rowspan= ' 2 ' > key cell </td>
&LT;TD colspan= ' 2 ' > Header text </td>
&LT;TD colspan= ' 2 ' > Header text </td>
<td> Table Header Text </td>
</tr>
<tr bgcolor= ' #FFF ' >
&LT;TD colspan= ' 2 ' > Header text </td>
&LT;TD rowspan= ' 2 ' > Header text </td>
&LT;TD colspan= ' 2 ' > Header text </td>
</tr>
<tr bgcolor= ' #FFF ' >
<td> Table Header Text </td>
<td> Table Header Text </td>
<td> Table Header Text </td>
<td> Table Header Text </td>
<td> header text </td> ";
</tr>
The above style can be set slash

Literal newcells = new Literal ();
Newcells.text = @ "Table Header text 1</th>
<th colspan= ' 2 ' > table header text 2</th>
<th colspan= ' 2 ' > table header text 3</th>
<th> Table Header Text 4</th>
</tr>
<tr bgcolor= ' "+ HeaderBackColor +" ' > ";
Newcells.text = @ "
<th colspan= ' 2 ' > table header text 5</th>
<th rowspan= ' 2 ' > table header text 6</th>
<th colspan= ' 2 ' > table header text 7</th>
</tr>
<tr bgcolor= ' "+ HeaderBackColor +" ' > ";
Newcells.text = @ "
<th> Table Header Text 8</th>
<th> Table Header Text 9</th>
<th> Table Header Text 10</th>
<th> Table Header Text 11</th>
<th> header Text 12 ";

Tablecellcollection cells = e.row.cells;
Tableheadercell Headercell = new Tableheadercell ();
The following property settings are consistent with the <TD rowspan= ' 2 ' > key cell </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)
  {
    if (e.row.rowtype = datacontrolrowtype.header)
    {
       E.row.attributes.add ("style", "background: #9999FF; color: #FFFFFF; font-size:14px");
   }
    Else
    {
      e.row.attributes.add ("style "," Background: #FFF ");
   }
 }
 
</script>

<title> add multi-layered header </title> to GridView
<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>



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.