ASP.NET 2.0中合并 GridView 的表头单元格 (新方法)

<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>

         ASP.NET 2.0中合并 GridView 的表头单元格 ,在网上看到孟子的文章后,觉得应该还有更直观的方法,在我的不断尝试之下,找到这个方法,就是在GridView的DataBind中编写代码,也可以达到同样合并表头的效果。不过代码看起来更直观了。

前台代码:

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

<! 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 >
            
< asp:GridView  ID ="GridView1"  runat ="server"  AutoGenerateColumns ="False"  CellPadding ="4"  CellSpacing ="1"  ForeColor ="#333333"  GridLines ="None"  OnDataBound ="GridView1_DataBound" >
                
< Columns >
                    
< asp:BoundField  HeaderText ="日期"  DataField ="Date"   />
                    
< asp:BoundField  HeaderText ="周次"  DataField ="WeekNo"   />
                    
< asp:BoundField  HeaderText ="浏览"  DataField ="Visit"   />
                    
< asp:BoundField  HeaderText ="人数"  DataField ="Hit"   />
                    
< asp:BoundField  HeaderText ="浏览"  DataField ="Visit1"   />
                    
< asp:BoundField  HeaderText ="人数"  DataField ="Hit1"   />
                    
< asp:BoundField  HeaderText ="浏览"  DataField ="Visit2"   />
                    
< asp:BoundField  HeaderText ="人数"  DataField ="Hit2"   />
                
</ Columns >
                
< FooterStyle  BackColor ="#990000"  Font-Bold ="True"  ForeColor ="White"   />
                
< RowStyle  BackColor ="#FFFBD6"  ForeColor ="#333333"   />
                
< SelectedRowStyle  BackColor ="#FFCC66"  Font-Bold ="True"  ForeColor ="Navy"   />
                
< PagerStyle  BackColor ="#FFCC66"  ForeColor ="#333333"  HorizontalAlign ="Center"   />
                
< HeaderStyle  BackColor ="#990000"  Font-Bold ="True"  ForeColor ="White"   />
                
< AlternatingRowStyle  BackColor ="White"   />
            
</ 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  testCells : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
this.GridView1.DataSource = this.CreateDataSource();
            
this.GridView1.DataBind();
        }


    }


    ICollection CreateDataSource()
    
{
        DataTable dt 
= new DataTable();
        DataColumn dc;
        DataRow dr;
        Random r 
= new Random(52);

        dc 
= new DataColumn("Date"typeof(System.DateTime));
        dc.DefaultValue 
= System.DateTime.Now.ToString("yyyy-MM-dd");
        dt.Columns.Add(dc);

        dc 
= new DataColumn("WeekNo"typeof(System.Int32));
        dt.Columns.Add(dc);

        dc 
= new DataColumn("Visit"typeof(System.Int32));
        dt.Columns.Add(dc);

        dc 
= new DataColumn("Hit"typeof(System.Int32));
        dt.Columns.Add(dc);

        dc 
= new DataColumn("Visit1"typeof(System.Int32));
        dt.Columns.Add(dc);

        dc 
= new DataColumn("Hit1"typeof(System.Int32));
        dt.Columns.Add(dc);


        dc 
= new DataColumn("Visit2"typeof(System.Int32));
        dt.Columns.Add(dc);

        dc 
= new DataColumn("Hit2"typeof(System.Int32));
        dt.Columns.Add(dc);


        
for (int i = 1; i < 100; i++)
        
{
            dr 
= dt.NewRow();

            dr[
0= System.DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
            dr[
1= r.Next(52);

            dr[
2= r.Next(5* i;
            dr[
3= r.Next(610* i;

            dr[
4= r.Next(11* i;
            dr[
5= r.Next(1120* i;

            dr[
6= r.Next(21* i;
            dr[
7= r.Next(3140* i;

            dt.Rows.Add(dr);
        }


        
return dt.DefaultView;

    }


    
protected void GridView1_DataBound(object sender, EventArgs e)
    
{

        GridViewRow gvr 
= new GridViewRow(00, DataControlRowType.Header, DataControlRowState.Normal);
        
//TableCellCollection tcc = null;
        Table t = new Table();
        TableRow tr 
= new TableRow();
        TableCell tc 
= null;
        tc 
= new TableCell();
        tc.ColumnSpan 
= 2;
        tc.HorizontalAlign 
= HorizontalAlign.Center;
        tc.Text 
= "";
        gvr.Cells.Add(tc);
        
//tr.Cells.Add(tc);


        tc 
= new TableCell();
        tc.ColumnSpan 
= 2;
        tc.Text 
= "合并1";
        tc.HorizontalAlign 
= HorizontalAlign.Center;
        gvr.Cells.Add(tc);
        
//tr.Cells.Add(tc);

        tc 
= new TableCell();
        tc.ColumnSpan 
= 2;
        tc.Text 
= "合并2";
        tc.HorizontalAlign 
= HorizontalAlign.Center;
        gvr.Cells.Add(tc);
        
//tr.Cells.Add(tc);

        tc 
= new TableCell();
        tc.ColumnSpan 
= 2;
        tc.Text 
= "合并3";
        tc.HorizontalAlign 
= HorizontalAlign.Center;
        gvr.Cells.Add(tc);
        
//tr.Cells.Add(tc);

        
//t.Rows.Add(tr);
        
//GridView1.Controls[0].Controls.AddAt(0, gvr);
        
//GridView1.HeaderRow.Controls.AddAt(0, gvr);
        
//e.Row.NamingContainer.Controls.AddAt(0, gvr);
        
//e.Row.Parent.Controls.AddAt(0, gvr);
        GridView1.HeaderRow.Parent.Controls.AddAt(0, gvr);//这样的语句是不是看起来更直观,一开始我在RowDataBound事件处理方法中写就会报错,说未引用对象,觉得应该能得到该对象的引用啊,我仔细想了之后觉得应该放在DataBound事件处理方法中就ok了,想想也是,在数据绑定之后肯定能得到gridview表头父控件的引用。
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值