ASP.NET 2.0中合并 GridView 的表头单元格 (新方法)
来源:互联网 发布:淘宝宝贝手机链接 编辑:程序博客网 时间:2024/05/17 21:59
<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>
<!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(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_DataBound(object sender, EventArgs e)
...{
GridViewRow gvr = new GridViewRow(0, 0, 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表头父控件的引用。
}
}
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_DataBound(object sender, EventArgs e)
...{
GridViewRow gvr = new GridViewRow(0, 0, 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表头父控件的引用。
}
}
- ASP.NET 2.0中合并 GridView 的表头单元格 (新方法)
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- 《ASP.NET 2.0中合并 GridView 的表头单元格》
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0中合并 GridView 的表头单元格
- dojo介绍
- Editplus中使用lua编译器的输出来定位出错行的正则表达式
- 第1天:选择什么样的DOCTYPE
- Java: 调试信息的输出 System.getProperty(String propertyName)
- 我的blog开张拉
- ASP.NET 2.0中合并 GridView 的表头单元格 (新方法)
- test
- winForm自定义控件攻略之一
- 学习内容
- 自动输出GridView的序号
- WinRunner测试设计(7)
- Java: 调试源代码 Using jdb
- 3D游戏中通用碰撞系统的构建
- CruiseControl 2.6.2简介及使用举例