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

<%...@ 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" OnRowDataBound="GridView1_RowDataBound" CellPadding="4" CellSpacing="1" ForeColor="#333333" GridLines="None">
<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(6, 10) * i;

dr[4] = r.Next(11) * i;
dr[5] = r.Next(11, 20) * i;

dr[6] = r.Next(21) * i;
dr[7] = r.Next(31, 40) * i;

dt.Rows.Add(dr);
}

return dt.DefaultView;

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

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

...{
e.Row.Cells[0].Text = Convert.ToDateTime(e.Row.Cells[0].Text).ToString("yyyy-MM-dd");
}

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

...{
e.Row.Cells[0].Text = "";

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

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

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

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

//下面是关键,孟子的思路就是获取当前gridview的第一个控件(即表头),然后在里面加上gridviewrow控件
// GridView1.Controls[0].Controls.AddAt(0, gvr);//孟子的方法
//我的方法其实差不多,就是通过获取当前表头控件所在父控件,然后在父控件中添加gridgiewrow
e.Row.Parent.Controls.AddAt(0, gvr);//我的方法
}
}
}
合并前效果:
合并后效果:
前台代码:



































后台代码:





































































































































