gridview 表头合并

本文详细介绍如何在ASP.NET中使用GridView实现多行表头合并效果,通过自定义Row_Created事件来绘制TableHeaderCell,并指定colspan或rowspan属性实现单元格的横向或竖向合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多行表头合并, 网上很多实例, 这里写的很详细, 力求让每个人都能看懂.

实现原理:GridView在ASP.NET中最终转为HMTL的表格显示表头。

在GridView创建行表头行时: e.Row.RowType == DatacontrolRowType.Header

清除掉旧的表头, 再重新拼接新的表头.

TableHeaderCell thc = new TableHeaderCell();

thc.Text = "表头";

对应生成的HTML为:<th>表头</th>

多行表头合并效果图

测试多行合并表头
表头表头1表头2表头3
表头1-1表头2-1表头2-2表头3-1表头3-2表头3-3

 1GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

 2GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客    GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客{

 3GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客        //判断创建的行是否为表头行

 4GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客        if (e.Row.RowType == DataControlRowType.Header)

 5GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客        GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客{

 6GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            //获取表头所在行的所有单元格

 7GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            TableCellCollection tcHeader = e.Row.Cells;

 8GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            //清除自动生成的表头

 9GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Clear();

10GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

11GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            //新添加的第一个表头单元格, 设置为合并7个列, 从而形成一行.

12GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

13GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[0].ColumnSpan = 7;

14GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[0].Text = "测试多行合并表头</th></tr><tr>";

15GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            //</th>表示当前单元格结束, </tr>表示本行结束, <tr>另起新一行    关键点

16GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            

17GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            //添加第二个表头单元格, 设置为合并两行.

18GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

19GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[1].RowSpan = 2;

20GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[1].Text = "表头";

21GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

22GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

23GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[2].Text = "表头1";

24GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

25GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

26GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[3].ColumnSpan = 2;

27GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[3].Text = "表头2";

28GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

29GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

30GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[4].ColumnSpan = 3;

31GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[4].Text = "表头3</th></tr><tr>";

32GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            

33GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            //第二行的所有的单元格添加完成, 换行</th></tr><tr>

34GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

35GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            //添加第三行所有的单元格

36GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客              tcHeader.Add(new TableHeaderCell());

37GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[5].Text = "表头1-1";

38GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

39GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

40GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[6].Text = "表头2-1";

41GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

42GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

43GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[7].Text = "表头2-2";

44GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

45GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

46GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[8].Text = "表头3-1";

47GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

48GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

49GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[9].Text = "表头3-2";

50GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

51GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader.Add(new TableHeaderCell());

52GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客            tcHeader[10].Text = "表头3-3</th></tr><tr>";

53GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客        }

54GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客

55GridView多行表头合并  - xiaozhu39505 - xiaozhu39505的博客    }


1、这里先介绍单纯的GridView多表头合并,先上图:


可以看到,上图就是生成的多表头,具体的后台代码是在Row_Created事件中创建的。先看创建代码:

复制代码
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) { case DataControlRowType.Header: //总表头 TableCellCollection tcHeader = e.Row.Cells; tcHeader.Clear(); //第一行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[0].Attributes.Add("bgcolor", "DarkSeaBlue"); tcHeader[0].Attributes.Add("colspan", "6"); //合并第一行的6列 tcHeader[0].Text = "用户基本信息</th></tr><tr>"; //第二行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[1].Attributes.Add("bgcolor", "DarkSeaGreen"); tcHeader[1].Attributes.Add("colspan", "3"); tcHeader[1].Text = "基本信息"; tcHeader.Add(new TableHeaderCell()); tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen"); tcHeader[2].Attributes.Add("colspan", "3"); tcHeader[2].Text = "籍贯信息</th></tr><tr>"; //第三行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[3].Attributes.Add("bgcolor", "Khaki"); tcHeader[3].Text = "公司"; tcHeader.Add(new TableHeaderCell()); tcHeader[4].Attributes.Add("bgcolor", "Khaki"); tcHeader[4].Text = "姓名"; tcHeader.Add(new TableHeaderCell()); tcHeader[5].Attributes.Add("bgcolor", "Khaki"); tcHeader[5].Text = "职衔"; tcHeader.Add(new TableHeaderCell()); tcHeader[6].Attributes.Add("bgcolor", "Khaki"); tcHeader[6].Text = "住址"; tcHeader.Add(new TableHeaderCell()); tcHeader[7].Attributes.Add("bgcolor", "Khaki"); tcHeader[7].Text = "城市"; tcHeader.Add(new TableHeaderCell()); tcHeader[8].Attributes.Add("bgcolor", "Khaki"); tcHeader[8].Text = "国籍"; break; } }
复制代码

需要说明的是,如果想实现多表头,得自己绘制tablecell,具体的方式就是先添加一个新的TableHeaderCell,然后就是设定其Attributes属性,再者如果要进行单元格合并,那么需要指定其colspan或者是rowspan,以便确定是横向合并还是竖向合并。

前台代码如下:

复制代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" GridLines="Vertical" Width="927px" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" onrowcreated="GridView1_RowCreated" ForeColor="Black"> <RowStyle BackColor="#F7F7DE" /> <Columns> <asp:BoundField DataField="CompanyName" HeaderText="公司" /> <asp:BoundField DataField="ContactName" HeaderText="姓名" /> <asp:BoundField DataField="ContactTitle" HeaderText="职衔" /> <asp:BoundField DataField="Address" HeaderText="住址" /> <asp:BoundField DataField="City" HeaderText="居住地" /> <asp:BoundField DataField="Country" HeaderText="国籍" SortExpression="Country" /> </Columns> <FooterStyle BackColor="#CCCC99" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Country] FROM [Customers]"> </asp:SqlDataSource>
复制代码


1、这里先介绍单纯的GridView多表头合并,先上图:

可以看到,上图就是生成的多表头,具体的后台代码是在Row_Created事件中创建的。先看创建代码:

复制代码
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) { case DataControlRowType.Header: //总表头 TableCellCollection tcHeader = e.Row.Cells; tcHeader.Clear(); //第一行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[0].Attributes.Add("bgcolor", "DarkSeaBlue"); tcHeader[0].Attributes.Add("colspan", "6"); //合并第一行的6列 tcHeader[0].Text = "用户基本信息</th></tr><tr>"; //第二行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[1].Attributes.Add("bgcolor", "DarkSeaGreen"); tcHeader[1].Attributes.Add("colspan", "3"); tcHeader[1].Text = "基本信息"; tcHeader.Add(new TableHeaderCell()); tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen"); tcHeader[2].Attributes.Add("colspan", "3"); tcHeader[2].Text = "籍贯信息</th></tr><tr>"; //第三行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[3].Attributes.Add("bgcolor", "Khaki"); tcHeader[3].Text = "公司"; tcHeader.Add(new TableHeaderCell()); tcHeader[4].Attributes.Add("bgcolor", "Khaki"); tcHeader[4].Text = "姓名"; tcHeader.Add(new TableHeaderCell()); tcHeader[5].Attributes.Add("bgcolor", "Khaki"); tcHeader[5].Text = "职衔"; tcHeader.Add(new TableHeaderCell()); tcHeader[6].Attributes.Add("bgcolor", "Khaki"); tcHeader[6].Text = "住址"; tcHeader.Add(new TableHeaderCell()); tcHeader[7].Attributes.Add("bgcolor", "Khaki"); tcHeader[7].Text = "城市"; tcHeader.Add(new TableHeaderCell()); tcHeader[8].Attributes.Add("bgcolor", "Khaki"); tcHeader[8].Text = "国籍"; break; } }
复制代码

需要说明的是,如果想实现多表头,得自己绘制tablecell,具体的方式就是先添加一个新的TableHeaderCell,然后就是设定其Attributes属性,再者如果要进行单元格合并,那么需要指定其colspan或者是rowspan,以便确定是横向合并还是竖向合并。

前台代码如下:

复制代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" GridLines="Vertical" Width="927px" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" onrowcreated="GridView1_RowCreated" ForeColor="Black"> <RowStyle BackColor="#F7F7DE" /> <Columns> <asp:BoundField DataField="CompanyName" HeaderText="公司" /> <asp:BoundField DataField="ContactName" HeaderText="姓名" /> <asp:BoundField DataField="ContactTitle" HeaderText="职衔" /> <asp:BoundField DataField="Address" HeaderText="住址" /> <asp:BoundField DataField="City" HeaderText="居住地" /> <asp:BoundField DataField="Country" HeaderText="国籍" SortExpression="Country" /> </Columns> <FooterStyle BackColor="#CCCC99" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Country] FROM [Customers]"> </asp:SqlDataSource>
复制代码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值