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. P4052 [JSOI2007]文本生成器

    P4052 [JSOI2007]文本生成器 AC自动机+dp 优秀题解传送门 设f[ i ][ j ]表示串的长度为 i ,当前在 j 点时不可识别的串的方案数 最后用总方案数减去不可识别方案数就是答 ...

  2. 常用maven命令总结

    常用Maven命令: mvn -v //查看版本 mvn archetype:create //创建 Maven 项目 mvn compile //编译源代码 mvn test-compile //编 ...

  3. UNIX网络编程--简介(一)【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/11760187 一.概述 a) 在编写与计算机通信的程序时,首先要确定的就是和计算机通信的 ...

  4. ubuntu启动google_chrome报错:FATAL:nss_util.cc(632)] NSS_VersionCheck("3.26") failed. NSS >= 3.26 is required. Please upgrade to the latest NSS

    一.背景: jello@jello:~$ lsb_release -aNo LSB modules are available.Distributor ID:    Ubuntu KylinDescr ...

  5. POJ 1236 Network of Schools(tarjan)题解

    题意:一个有向图.第一问:最少给几个点信息能让所有点都收到信息.第二问:最少加几个边能实现在任意点放信息就能传遍所有点 思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度 tarjan算法:问 ...

  6. 解决Eclipse 项目报错:Unbound classpath container: ‘JRE System Library [JavaSE-1.7]

    MyEclipse出现下面两条报错: The project cannot be built until build path errors are resolved HelloWord Unknow ...

  7. Linux mysql 添加远程连接

    方法/步骤 第一步 远程连接上Linux系统,确保Linux系统已经安装上了MySQL数据库.登陆数据库. mysql -u$user -p $pwd 第二步 创建用户用来远程连接 GRANT ALL ...

  8. C#学习笔记(九):函数、代码查询和调试

    代码查询和调试 代码查询 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  9. HDU 3065 病毒侵袭持续中(AC自动机(每个模式串出现次数))

    http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:求每个模式串出现的次数. 思路: 不难,把模板修改一下即可. #include<iostrea ...

  10. 关于C++中的友元函数的总结

    1.友元函数的简单介绍 1.1为什么要使用友元函数 在实现类之间数据共享时,减少系统开销,提高效率.如果类A中的函数要访问类B中的成员(例如:智能指针类的实现),那么类A中该函数要是类B的友元函数.具 ...