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. HTTP从入门到入土(5)——HTTP报文格式

    HTTP报文格式 HTTP报文分为请求报文和响应报文,只有发送了请求报文,才会有响应报文. 常见的报文格式如下所示:

  2. Eclipse中的工程引入jar包后没有整合到一个文件夹而是全部在根目录下显示

    Eclipse中的工程引入jar包后没有整合到一个文件夹而是全部在根目录下显示 解决方案: 1,在Eclipse中,点击window-->Preferences-->Java-->B ...

  3. 在wamp 2.0环境下面安装Zend Optimizer的方法

    原文链接:http://blog.sina.com.cn/s/blog_8dc13ec50101pbat.html 我是用WAMP来做PHP的服务器,进行本机测试和开发PHP项目. wamp环境是刚刚 ...

  4. Socket:读写处理及连接断开的检测

    作为进程间通信及网络通信的一种重要技术,在实际的开发中,socket编程是经常被用到的.关于socket编程的一般步骤,这里不再赘述,相关资料和文章很多,google/baidu即可. 本文主要是探讨 ...

  5. C++ 一串数字三位一节,用逗号隔开表示

    #include <iostream> #include <string> #include <sstream> using namespace std; stri ...

  6. Xcode基本设置系列和Xcode报错解决方案

    1, arc机制中调用非arc文件. Xcode——>Project->Build Phases,将需要非arc文件更改为:"-fno-objc-arc"   ,该参数 ...

  7. HDU 6171 Admiral(双向BFS+队列)题解

    思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...

  8. C# 测算代码运行时间 Stopwatch

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); stop ...

  9. makefile 中的符号替换($@、$^、$<、$?)

    Makefile  $@, $^, $< $@  表示目标文件$^  表示所有的依赖文件$<  表示第一个依赖文件$?  表示比目标还要新的依赖文件列表 如一个目录下有如下文件: $ ls ...

  10. Linux 常用的压缩命令有 gzip 和 zip

    Linux 常用的压缩命令有 gzip 和 zip,两种压缩包的结尾不同:zip 压缩的后文件是 *.zip ,而 gzip 压缩后的文件 *.gz 相应的解压缩命令则是 gunzip 和 unzip ...