事件:gridview_RowCreated
代码:
switch (e.Row.RowType)
{
case DataControlRowType.Header://类型为header
//第一行表头
TableCellCollection tcHeader = e.Row.Cells;//TableCellCollection单元格的集合
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("bgcolor", "Red");
tcHeader[0].Attributes.Add("colspan", "4");//占4个列
tcHeader[0].Text = "全部信息</tr><tr>";
tcHeader[0].HorizontalAlign = HorizontalAlign.Center;//居中显示
//第二行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[1].Attributes.Add("rowspan", "2");
tcHeader[1].Text = "主键";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("bgcolor", "LightSteelBlue");
tcHeader[2].Attributes.Add("colspan", "3");
tcHeader[2].Text = "基本信息</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 = "出生日期</tr>";
break;
}
结果:

本文介绍如何使用ASP.NET中的GridView控件自定义其表头样式。通过在gridview_RowCreated事件中设置不同类型的表头,包括背景颜色、跨列显示、文字内容等属性,实现复杂且美观的表头布局。

1166

被折叠的 条评论
为什么被折叠?



