[ASP.NET][WebControl] GridView多重表頭
GirdView呈現的表頭,我想要調整有多個表頭
要在RowCreated事件中進行
{
if (e.Row.RowType == DataControlRowType.Header)
{
//將原有的表頭移除
TableCellCollection oldCell = e.Row.Cells;
oldCell.Clear();
#region 第一列
//多重表頭的第一列
GridViewRow gvRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
//第一欄
TableCell tc = new TableCell();
tc.Text = "大大大標題";
tc.BackColor = System.Drawing.Color.AliceBlue; //背景色彩
tc.HorizontalAlign = HorizontalAlign.Center;
tc.VerticalAlign = VerticalAlign.Middle;
tc.Width = 160;
tc.RowSpan = 1; //所跨的row數
tc.ColumnSpan = 2; //所跨的column數
gvRow.Cells.Add(tc); //新增
//第二欄
tc = new TableCell();
tc.Text = "貨物資料";
tc.BackColor = System.Drawing.Color.AliceBlue;
tc.Width = 160;
tc.RowSpan = 2;
tc.ColumnSpan = 2;
tc.HorizontalAlign = HorizontalAlign.Center;
gvRow.Cells.Add(tc);
//新增至GridView
GridView1.Controls[0].Controls.Add(gvRow);
#endregion
#region 第二列
//多重表頭的第二列
gvRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
tc = new TableCell();
tc.HorizontalAlign = HorizontalAlign.Center;
tc.Text = "編號";
tc.Width = 80;
tc.BackColor = System.Drawing.Color.FromName("red");
gvRow.Cells.Add(tc);
tc = new TableCell();
tc.HorizontalAlign = HorizontalAlign.Center;
tc.Text = "客戶";
tc.Width = 80;
tc.BackColor = System.Drawing.Color.BlueViolet;
gvRow.Cells.Add(tc);
GridView1.Controls[0].Controls.Add(gvRow);
#endregion
}
}
結果呈現
範例檔案 下載