gridview 复杂头 实现 固定

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

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

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

public partial class Modules_test2 : System.Web.UI.Page
{
    ICollection CreateDataSource()
    
{
        System.Data.DataTable dt 
= new System.Data.DataTable();
        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.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++)
        
{
            System.Random rd 
= new System.Random(Environment.TickCount * i); ;
            dr 
= dt.NewRow();
            dr[
0= "班级" + i.ToString();
            dr[
1= "学生" + i.ToString();
            dr[
2= System.Math.Round(rd.NextDouble() * 1002);
            dr[
3= System.Math.Round(rd.NextDouble() * 1002);
            dr[
4= System.Math.Round(rd.NextDouble() * 1002);
            dr[
5= System.Math.Round(rd.NextDouble() * 1002);
            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)
        
{
            
//创建一个GridViewRow,相当于表格的 TR 一行
            GridViewRow rowHeader = new GridViewRow(00, DataControlRowType.Header, DataControlRowState.Normal);
            
string HeaderBackColor = "#EDEDED";
            rowHeader.BackColor 
= System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
            
string thstyle = "style='position: relative; top:expression(this.offsetParent.scrollTop - 1); table-layout:fixed;z-index: 10;'";

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

            TableCellCollection cells 
= e.Row.Cells;
            TableHeaderCell headerCell 
= new TableHeaderCell();
            
//下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
            headerCell.RowSpan = 2;
            headerCell.Controls.Add(newCells);
            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.Visible 
= true;
            
//添加到 GridView1
            GridView1.Controls[0].Controls.AddAt(0, rowHeader);

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

            
//TableCellCollection tcl = e.Row.Cells;
            ////清除自动生成的表头
            //tcl.Clear();
            ////添加新的表头
            //tcl.Add(new TableHeaderCell());
            
//tcl[0].RowSpan = 2;
            
//tcl[0].Text = "标题s";
            
//Label l1 = new Label();
            
//l1.Text = "uu";
            
//Button bt = new Button();
            
//bt.Text = "1";
            ////因为自定义表头,所以原来系统中实现的排序功能就失效了
            
////下面语句可以自己在表头中添加控件,自己实现排序功能

            //tcl[0].Controls.Add(l1);
            
//tcl[0].Controls.Add(bt);

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

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

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

            
//tcl.Add(new TableHeaderCell());
            
//tcl[4].RowSpan = 2;
            ////这段是重点 其实在生成的html中tcl[4]转化为<th>标题5</th>
            
////所以依照该原则注入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";

        }

    }


    
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;CellSpacing:0;");
        }

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

        }

    }

}

posted on 2007-11-26 20:20  执法长老  阅读(358)  评论(0编辑  收藏  举报

导航