protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
rowHeader.BackColor = System.Drawing.Color.White;
rowHeader.Font.Bold = true;
TableCellCollection cells = e.Row.Cells;
TableCell headerCell = new TableCell();
headerCell.Text = "";
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "学生成绩";
headerCell.ColumnSpan = cells.Count - 1;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}
原理:只需让表元跨多列就可以了。