最近的項目中,用戶需求有如下表頭的表格:
盡管GridView的屬性編輯器內沒有直接提供合並的操作,但通過後台的代碼,還是可以實現這樣的效果的。代碼寫在GridVIEw的RowCreated事件中。
protected void GridView1_RowCreated(object sender, GridVIEwRowEventArgs e)
...{
if (e.Row.RowType == DataControlRowType.Header)
...{
GridViewRow rowHeader = new GridVIEwRow(1, 0, DataControlRowType.Header, DataControlRowState.Normal);
rowHeader.BackColor = System.Drawing.Color.LightBlue;
rowHeader.Font.Bold = true;
TableCellCollection cells = e.Row.Cells;
TableCell headerCell = new TableCell();
headerCell = new TableCell();
headerCell.Text = "總調與中調計算機通信";
headerCell.ColumnSpan = 2
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "自動發電控制";
headerCell.ColumnSpan = 3;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
GridVIEw1,Controls[0].Controls.AddAt(0, rowHeader);
}
}