如何自定义 GridView 表头
关键:GridView 控件的 RowCreated 事件,TableCellCollection 类,表头换行使用"</th></
tr><tr>"
步骤 1:自定义方法 gv_header,代码如下。
/// <summary>
/// 自定义 GridView 表头
/// </summary>
/// <param name="e"></param>
private void gv_header(GridViewRowEventArgs e)
{
string cell_color = "white";
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
//第一行
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("rowspan", "2"); //跨 Row
tcHeader[0].Attributes.Add("bgcolor", cell_color);
tcHeader[0].Attributes.Add("style", "color:black");
tcHeader[0].Text = "区域";
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("colspan", "3"); //跨 Column
tcHeader[1].Attributes.Add("bgcolor", cell_color);
tcHeader[1].Attributes.Add("style", "color:black");
tcHeader[1].Text = "A 级";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("colspan", "3"); //跨 Column
tcHeader[2].Attributes.Add("bgcolor", cell_color);
tcHeader[2].Attributes.Add("style", "color:black");
tcHeader[2].Text = "B 级";
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("colspan", "3"); //跨 Column
tcHeader[3].Attributes.Add("bgcolor", cell_color);
tcHeader[3].Attributes.Add("style", "color:black");
tcHeader[3].Text = "C 级";
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("colspan", "3"); //跨 Column
tcHeader[4].Attributes.Add("bgcolor", cell_color);
tcHeader[4].Attributes.Add("style", "color:black");
tcHeader[4].Text = "其他级" + "</th></tr><tr>";
//第二行
int i = 5;
for (int v = 0; v < 4; v++)