 
                    
                
         
    
    
    
    
    
        
            
                
    
        
        
                
        
        还不完善,在空隙有字,待以后解决吧!
    

 <%
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="Modules_test2" %>
@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="Modules_test2" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml" >
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
<head runat="server">
 <title>无标题页</title>
    <title>无标题页</title>
 </head>
</head>
 <body>
<body>
 <form id="form1" runat="server">
    <form id="form1" runat="server">
 <div id="divgrid" style="overflow-y: scroll; height: 200px; width: 300px;">
    <div id="divgrid" style="overflow-y: scroll; height: 200px; width: 300px;">
 <asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px"
        <asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px" 
 Width="600px"  BorderWidth="0" OnRowDataBound="GridView1_RowDataBound"
     Width="600px"  BorderWidth="0" OnRowDataBound="GridView1_RowDataBound"
 OnRowCreated="GridView1_RowCreated" ShowHeader="false">
      OnRowCreated="GridView1_RowCreated" ShowHeader="false">
 </asp:GridView>
   </asp:GridView>
 </div>
    </div>
 </form>
    </form>
 </body>
</body>
 </html>
</html>

 using System;
using System;
 using System.Data;
using System.Data;
 using System.Configuration;
using System.Configuration;
 using System.Collections;
using System.Collections;
 using System.Web;
using System.Web;
 using System.Web.Security;
using System.Web.Security;
 using System.Web.UI;
using System.Web.UI;
 using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;

 public partial class Modules_test2 : System.Web.UI.Page
public partial class Modules_test2 : System.Web.UI.Page


 {
{
 ICollection CreateDataSource()
    ICollection CreateDataSource()

 
     {
{
 System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataTable dt = new System.Data.DataTable();
 System.Data.DataRow dr;
        System.Data.DataRow dr;
 dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
 dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
 dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
 dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
 dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
 dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));

 for (int i = 0; i < 40; i++)
        for (int i = 0; i < 40; i++)

 
         {
{
 System.Random rd = new System.Random(Environment.TickCount * i); ;
            System.Random rd = new System.Random(Environment.TickCount * i); ;
 dr = dt.NewRow();
            dr = dt.NewRow();
 dr[0] = "班级" + i.ToString();
            dr[0] = "班级" + i.ToString();
 dr[1] = "学生" + i.ToString();
            dr[1] = "学生" + i.ToString();
 dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
            dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
 dr[3] = 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[4] = System.Math.Round(rd.NextDouble() * 100, 2);
 dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
            dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
 dt.Rows.Add(dr);
            dt.Rows.Add(dr);
 }
        }
 System.Data.DataView dv = new System.Data.DataView(dt);
        System.Data.DataView dv = new System.Data.DataView(dt);
 return dv;
        return dv;
 }
    }

 protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e)

 
     {
{
 if (!IsPostBack)
        if (!IsPostBack)

 
         {
{
 GridView1.BorderColor = System.Drawing.Color.DarkOrange;
            GridView1.BorderColor = System.Drawing.Color.DarkOrange;
 GridView1.DataSource = CreateDataSource();
            GridView1.DataSource = CreateDataSource();
 GridView1.DataBind();
            GridView1.DataBind();
 }
        }
 }
    }

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

 
     {
{

 if (e.Row.RowType == DataControlRowType.Header)
        if (e.Row.RowType == DataControlRowType.Header)

 
         {
{
 //创建一个GridViewRow,相当于表格的 TR 一行
            //创建一个GridViewRow,相当于表格的 TR 一行
 GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
 string HeaderBackColor = "#EDEDED";
            string HeaderBackColor = "#EDEDED";
 rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
            rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
 string thstyle = "style='position: relative; top:expression(this.offsetParent.scrollTop - 1); table-layout:fixed;z-index: 10;'";
            string thstyle = "style='position: relative; top:expression(this.offsetParent.scrollTop - 1); table-layout:fixed;z-index: 10;'";

 Literal newCells = new Literal();
            Literal newCells = new Literal();
 newCells.Text = @"表头文字1</th>
            newCells.Text = @"表头文字1</th>
 <th " + thstyle + @" colspan='2'>表头文字2</th>
                  <th " + thstyle + @" colspan='2'>表头文字2</th>
 <th " + thstyle + @" colspan='2'>表头文字3</th>
                  <th " + thstyle + @" colspan='2'>表头文字3</th>
 <th " + thstyle + @">表头文字4</th>
                  <th " + thstyle + @">表头文字4</th>
 </tr>
                  </tr>
 <tr bgcolor='" + HeaderBackColor + "'>";
                  <tr bgcolor='" + HeaderBackColor + "'>";
 newCells.Text += @"
            newCells.Text += @"                         
 <th " + thstyle + @" colspan='2'>表头文字5</th>
                  <th " + thstyle + @" colspan='2'>表头文字5</th>
 <th " + thstyle + @" rowspan='2'>表头文字6</th>
                  <th " + thstyle + @" rowspan='2'>表头文字6</th>
 <th " + thstyle + @" colspan='2'>表头文字7</th>
                  <th " + thstyle + @" colspan='2'>表头文字7</th>
 </tr>
                  </tr>
 <tr bgcolor='" + HeaderBackColor + "'>";
                  <tr bgcolor='" + HeaderBackColor + "'>";
 newCells.Text += @"
            newCells.Text += @"  
 <th " + thstyle + @">表头文字8</th>
                  <th " + thstyle + @">表头文字8</th>
 <th " + thstyle + @">表头文字9</th>
                  <th " + thstyle + @">表头文字9</th>
 <th " + thstyle + @">表头文字10</th>
                  <th " + thstyle + @">表头文字10</th>
 <th " + thstyle + @">表头文字11</th>
                  <th " + thstyle + @">表头文字11</th>
 <th " + thstyle + @">表头文字12";
                  <th " + thstyle + @">表头文字12";

 TableCellCollection cells = e.Row.Cells;
            TableCellCollection cells = e.Row.Cells;
 TableHeaderCell headerCell = new TableHeaderCell();
            TableHeaderCell headerCell = new TableHeaderCell();
 //下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
            //下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
 headerCell.RowSpan = 2;
            headerCell.RowSpan = 2;
 headerCell.Controls.Add(newCells);
            headerCell.Controls.Add(newCells);
 headerCell.Attributes.Add("style", "position: relative; top:expression(this.offsetParent.scrollTop - 1); table-layout:fixed;z-index: 10;");
            headerCell.Attributes.Add("style", "position: relative; top:expression(this.offsetParent.scrollTop - 1); table-layout:fixed;z-index: 10;");
 rowHeader.Cells.Add(headerCell);
            rowHeader.Cells.Add(headerCell);

 rowHeader.Cells.Add(headerCell);
            rowHeader.Cells.Add(headerCell);
 rowHeader.Visible = true;
            rowHeader.Visible = true;
 //添加到 GridView1
            //添加到 GridView1
 GridView1.Controls[0].Controls.AddAt(0, rowHeader);
            GridView1.Controls[0].Controls.AddAt(0, rowHeader);

 //TableCellCollection cells = e.Row.Cells;
            //TableCellCollection cells = e.Row.Cells;
 //TableCell tc = new TableCell();
            //TableCell tc = new TableCell();
 //tc.Text = newCells.Text;
            //tc.Text = newCells.Text;
 //tc.Attributes.Add("style", "position: relative; top: table-layout:fixed;expression(this.offsetParent.scrollTop);z-index: 10;");
            //tc.Attributes.Add("style", "position: relative; top: table-layout:fixed;expression(this.offsetParent.scrollTop);z-index: 10;");
 //rowHeader.Cells.Add(tc);
            //rowHeader.Cells.Add(tc);
 //GridView1.Controls[0].Controls.AddAt(0, rowHeader);
            //GridView1.Controls[0].Controls.AddAt(0, rowHeader);

 //TableCellCollection tcl = e.Row.Cells;
            //TableCellCollection tcl = e.Row.Cells;

 /**/////清除自动生成的表头
            /**/////清除自动生成的表头
 //tcl.Clear();
            //tcl.Clear();

 /**/////添加新的表头
            /**/////添加新的表头
 //tcl.Add(new TableHeaderCell());
            //tcl.Add(new TableHeaderCell());
 //tcl[0].RowSpan = 2;
            //tcl[0].RowSpan = 2;
 //tcl[0].Text = "标题s";
            //tcl[0].Text = "标题s";
 //Label l1 = new Label();
            //Label l1 = new Label();
 //l1.Text = "uu";
            //l1.Text = "uu";
 //Button bt = new Button();
            //Button bt = new Button();
 //bt.Text = "1";
            //bt.Text = "1";

 /**/////因为自定义表头,所以原来系统中实现的排序功能就失效了
            /**/////因为自定义表头,所以原来系统中实现的排序功能就失效了
 ////下面语句可以自己在表头中添加控件,自己实现排序功能
            ////下面语句可以自己在表头中添加控件,自己实现排序功能
 //tcl[0].Controls.Add(l1);
            //tcl[0].Controls.Add(l1);
 //tcl[0].Controls.Add(bt);
            //tcl[0].Controls.Add(bt);

 //tcl.Add(new TableHeaderCell());
            //tcl.Add(new TableHeaderCell());
 //tcl[1].ColumnSpan = 2;
            //tcl[1].ColumnSpan = 2;
 //tcl[1].Text = "标题2";
            //tcl[1].Text = "标题2";

 //tcl.Add(new TableHeaderCell());
            //tcl.Add(new TableHeaderCell());
 //tcl[2].RowSpan = 2;
            //tcl[2].RowSpan = 2;
 //tcl[2].Text = "标题3";
            //tcl[2].Text = "标题3";

 //tcl.Add(new TableHeaderCell());
            //tcl.Add(new TableHeaderCell());
 //tcl[3].ColumnSpan = 3;
            //tcl[3].ColumnSpan = 3;
 //tcl[3].Text = "标题4";
            //tcl[3].Text = "标题4";

 //tcl.Add(new TableHeaderCell());
            //tcl.Add(new TableHeaderCell());
 //tcl[4].RowSpan = 2;
            //tcl[4].RowSpan = 2;

 /**/////这段是重点 其实在生成的html中tcl[4]转化为<th>标题5</th>
            /**/////这段是重点 其实在生成的html中tcl[4]转化为<th>标题5</th>
 ////所以依照该原则注入html标签来实现,原理有些类似于SQL注入攻击
            ////所以依照该原则注入html标签来实现,原理有些类似于SQL注入攻击
 //tcl[4].Text = "标题5</th></tr><tr><th>标题2-1</th><th>标题2-2</th><th>标题4-1</th><th>标题4-2</th><th>标题4-3";
            //tcl[4].Text = "标题5</th></tr><tr><th>标题2-1</th><th>标题2-2</th><th>标题4-1</th><th>标题4-2</th><th>标题4-3";

 }
        }
 }
    }

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

 
     {
{
 if (e.Row.RowType == DataControlRowType.Header)
        if (e.Row.RowType == DataControlRowType.Header)

 
         {
{
 e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px;CellSpacing:0;");
            e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px;CellSpacing:0;");
 }
        }
 else
        else

 
         {
{
 e.Row.Attributes.Add("style", "background:#FFF;");
            e.Row.Attributes.Add("style", "background:#FFF;");

 }
        }
 }
    }
 }
}
