protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection tcHeader = e.Row.Cells;
//获取表头所在行的所有单元格 //清除自动生成的表头
tcHeader.Clear(); //新添加的第一个表头单元格, 设置为合并7个列, 从而形成一行.
tcHeader.Add(new TableHeaderCell());
tcHeader[0].ColumnSpan = 7;
tcHeader[0].Text = "测试多行合并表头</th></tr><tr>";
//</th>表示当前单元格结束, </tr>表示本行结束, <tr>另起新一行 关键点 //添加第二个表头单元格, 设置为合并两行.
tcHeader.Add(new TableHeaderCell());
tcHeader[1].RowSpan = 2;
tcHeader[1].Text = "表头";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Text = "表头1"; tcHeader.Add(new TableHeaderCell());
tcHeader[3].ColumnSpan = 2;
tcHeader[3].Text = "表头2"; tcHeader.Add(new TableHeaderCell());
tcHeader[4].ColumnSpan = 3;
tcHeader[4].Text = "表头3</th></tr><tr>";
//第二行的所有的单元格添加完成, 换行</th></tr><tr> //添加第三行所有的单元格
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Text = "表头1-1"; tcHeader.Add(new TableHeaderCell());
tcHeader[6].Text = "表头2-1"; tcHeader.Add(new TableHeaderCell());
tcHeader[7].Text = "表头2-2"; tcHeader.Add(new TableHeaderCell());
tcHeader[8].Text = "表头3-1"; tcHeader.Add(new TableHeaderCell());
tcHeader[9].Text = "表头3-2"; tcHeader.Add(new TableHeaderCell());
tcHeader[10].Text = "表头3-3</th></tr><tr>"; } }

第二种方法

protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e )
{ if (e.Row.RowType == DataControlRowType.Header)
{
//创建一个GridViewRow,相当于表格的 TR 一行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor); //实现确定要显示的表头样式,也可以通过计算生成 // <tr>
// <td rowspan='2'>关键单元格</td>
// <td colspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// <td>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td colspan='2'>表头文字</td>
// <td rowspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>";
// </tr>
// 上面的样式可以设置斜线 Literal newCells = new Literal();
newCells.Text = @"表头文字1</th>
<th colspan='2'>表头文字2</th>
<th colspan='2'>表头文字3</th>
<th>表头文字4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>表头文字5</th>
<th rowspan='2'>表头文字6</th>
<th colspan='2'>表头文字7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>表头文字8</th>
<th>表头文字9</th>
<th>表头文字10</th>
<th>表头文字11</th>
<th>表头文字12"; TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
//下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell); rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true; //添加到 GridView1
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}

asp.net GridView多行表头的实现,合并表头的更多相关文章

  1. 如何让Gridview在没有数据的时候显示表头(asp.net)

    原文:如何让Gridview在没有数据的时候显示表头(asp.net) 1.前言 当对GridView控件进行数据绑定时,如果绑定的记录为空,网页上就不显示GridView,造成页面部分空白,页面布局 ...

  2. Asp.net GridView 72般绝技

    快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...

  3. GridView/DataGrid行单击和双击事件实现代码_.Net教程

    功能: 单击选中行,双击打开详细页面 说明:单击事件(onclick)使用了 setTimeout 延迟,根据实际需要修改延迟时间 ;当双击时,通过全局变量 dbl_click 来取消单击事件的响应  ...

  4. GRIDVIEW多行多列合并单元格(合并列)

    GitHub项目地址:https://github.com/mingceng/merge-gridviewcell 去年的时候,我写了两篇文章:  GridView多行多列合并单元格(完整代码和例子) ...

  5. 【DevExpress v17.2新功能预告】增强ASP.NET GridView的功能

    在下一个主要版本v17.2中,我们将为DevExpress ASP.NET GridView添加一些优秀的新功能.在本文中为大家介绍的所有功能都可用于 GridView的ASP.NET WebForm ...

  6. DEV express 对Gridview某行的元素赋值

    1:获取选中的行某列的值 string colValue= this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.g ...

  7. gridview的行选择的一个问题

    我想实现这样一个效果:单击gridview的行内任意地方都可以选择该行(就是行内复选框被选中),同时修改该行的背景色.当再次单击行内任意地方又可以取消选择.另外,当单击选择行内复选框时,我希望可以选择 ...

  8. asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台

    分享: 腾讯微博  新浪微博   搜狐微博   网易微博  腾讯朋友  百度贴吧  豆瓣   QQ好友  人人网 作者:王春天  原文地址:http://www.cnblogs.com/spring_ ...

  9. ASP.new GridView获取隐藏列值的几种方法

    解决方法: 原文来自:http://www.tzwhx.com/NewShow/newBodyShow/控件_32933.html 作者:lerit  1.隐藏列前获取数据 看这样一个例子(以下均以此 ...

随机推荐

  1. 写给java程序员的c++与java实现的一些重要细微差别-附完整版pdf学习手册

    0.其实常规的逻辑判断结构.工具类.文件读写.控制台读写这些的关系都不大,熟悉之后,这些都是灵活运用的问题. 学习c/c++需要预先知道的一个前提就是,虽然有ANSI C标准,但是每个c/c++编译器 ...

  2. 20165211 2017-2018-2 《Java程序设计》第7周学习总结

    20165211 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 本周,我学习了书本上第十一章的内容,以下是我整理的主要知识. 第十一章 JDBC和MySQL ...

  3. linux内核分析 第六周

    一.进程的描述 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. 1.进程控制块PCB--task_struct 进程状态 进程打开的文件 进程优先级信息 2. ...

  4. 在浏览器输入url后并回车发生了哪些过程

    1.解析URL ________________________________________________________________________ 关于URL: URL(Universa ...

  5. tomcat部署项目如何去掉项目名称

    去掉项目名和端口: 首先,进入tomcat的安装目录下的conf目录,我的目录是 /usr/local/apache-tomcat-6.0.20/conf,编辑文件server.xml. 1.去除端口 ...

  6. P4113 [HEOI2012]采花 (莫队TLE)

    思路 update 11.2 树状数组AC 本题莫队过不去,会TLE ----------------------- 但也是个不错的莫队练手题 ------------------------ 毕竟C ...

  7. label表单的关联性

    <input type="checkbox" id="cr" /> <label for="cr">点击关联复选框& ...

  8. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼

    起初这样能短暂解决问题,后来发现每次机器重启了就还是有这样的错误,还是要执行SQL,很麻烦: show variables like '%time_zone%'; select now(); set ...

  9. kafka删除一个topic

    前言 当我们在shell中执行topic删除命令的时候` kafka-topics --delete --topic xxxx --zookeeper xxx`,会显示,xxxx已经被标记为删除.然后 ...

  10. MySql 插入数据返回数据的Id值

    insert into addeditemgroup(addeditemgroupname) value(') ; select @@IDENTITY  as id; 返回最新的Id: