需要实现的效果

通过GridView的OnRowCreated事件实现
/// <summary>
/// 行创建事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvInfo_RowCreated(object sender, GridViewRowEventArgs e)
{
DataControlRowType rowType = e.Row.RowType;
if(rowType== DataControlRowType.Header)
{
//第一行表头
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Text = "队组";
tcHeader[0].Font.Bold = true;//加粗
tcHeader[0].Attributes.Add("rowspan", "2"); //跨Row
tcHeader[0].Attributes.Add("style", "width:150px;padding:20px 8px"); //样式设置
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Text = "月计划";
tcHeader[1].Font.Bold = true;//加粗
tcHeader[1].Attributes.Add("rowspan", "2"); //跨Row
tcHeader[1].Attributes.Add("style", "width:120px;padding:20px 8px"); //样式设置
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Text = "当日";
tcHeader[2].Font.Bold = true;//加粗
tcHeader[2].Attributes.Add("colspan", "3"); //跨Column
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Text = "月累计</tr><tr style='background-color:WhiteSmoke;'>";//<tr>
tcHeader[3].Font.Bold = true;//加粗
tcHeader[3].Attributes.Add("colspan", "3"); //跨Column
//第二行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Text = "计划";
tcHeader[4].Font.Bold = true;//加粗
tcHeader[4].Attributes.Add("style", "width:120px"); //宽度设置
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Text = "实际";
tcHeader[5].Font.Bold = true;//加粗
tcHeader[5].Attributes.Add("style", "width:120px"); //宽度设置
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Text = "超亏";
tcHeader[6].Font.Bold = true;//加粗
tcHeader[6].Attributes.Add("style", "width:120px"); //宽度设置
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Text = "计划";
tcHeader[7].Font.Bold = true;//加粗
tcHeader[7].Attributes.Add("style", "width:120px"); //宽度设置
tcHeader.Add(new TableHeaderCell());
tcHeader[8].Text = "实际";
tcHeader[8].Font.Bold = true;//加粗
tcHeader[8].Attributes.Add("style", "width:120px"); //宽度设置
tcHeader.Add(new TableHeaderCell());
tcHeader[9].Text = "超亏";
tcHeader[9].Font.Bold = true;//加粗
tcHeader[9].Attributes.Add("style", "width:120px"); //宽度设置
}
}
本文介绍如何使用ASP.NET中的GridView控件自定义表头样式,包括行创建事件gvInfo_RowCreated的具体实现,以及如何设置表头单元格的文本、字体样式、跨行和跨列属性。

290

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



