GridView实现多表头合并
发布日期:2021-05-14 04:36:44 浏览次数:16 分类:博客文章

本文共 6351 字,大约阅读时间需要 21 分钟。

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

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

View Code
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
=
"
用户基本信息
"
;
//
第二行表头
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
=
"
籍贯信息
"
;
//
第三行表头
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,以便确定是横向合并还是竖向合并。

前台代码如下:

View Code
<
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
"
/>
<
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:SqlDataSource ID
=
"
SqlDataSource1
"
runat
=
"
server
"
ConnectionString
=
"
<%$ ConnectionStrings:NorthwindConnectionString %>
"
SelectCommand
=
"
SELECT [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Country] FROM [Customers]
"
>

希望对你有用,谢谢。

上一篇:asp.net下载网络文件
下一篇:DotNet操作Oracle存储过程备忘

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月28日 13时37分48秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章