GridView的表頭跨欄設計

GridView的表頭跨欄設計

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        #region "GridView的表頭跨欄設計"
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection oldCell = e.Row.Cells;
            oldCell.Clear();//將原有的表頭格式移除
            e.Row.Visible = false;
 
            #region "產生多重表列的第一列"
            GridViewRow headRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            //第一欄
            TableCell head2Cell = new TableCell();
            head2Cell.Text = "招攬行員";
            head2Cell.BackColor = Color.LightSteelBlue;  
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.RowSpan = 2;//所跨的欄數
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
 
            //第二欄
            head2Cell = new TableCell();
            head2Cell.Text = "1月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第三欄
            head2Cell = new TableCell();
            head2Cell.Text = "2月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第四欄
            head2Cell = new TableCell();
            head2Cell.Text = "3月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第五欄
            head2Cell = new TableCell();
            head2Cell.Text = "4月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第六欄
            head2Cell = new TableCell();
            head2Cell.Text = "5月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第七欄
            head2Cell = new TableCell();
            head2Cell.Text = "6月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第八欄
            head2Cell = new TableCell();
            head2Cell.Text = "7月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第九欄
            head2Cell = new TableCell();
            head2Cell.Text = "8月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第十欄
            head2Cell = new TableCell();
            head2Cell.Text = "9月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第十一欄
            head2Cell = new TableCell();
            head2Cell.Text = "10月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第十二欄
            head2Cell = new TableCell();
            head2Cell.Text = "11月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第十三欄
            head2Cell = new TableCell();
            head2Cell.Text = "12月";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第十四欄
            head2Cell = new TableCell();
            head2Cell.Text = "全年";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 3;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第十五欄
            head2Cell = new TableCell();
            head2Cell.Text = "累積開卡率";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 1;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.RowSpan = 2;//所跨的欄數
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //第十六欄
            head2Cell = new TableCell();
            head2Cell.Text = "累積動用率";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.ColumnSpan = 1;//所跨的欄數
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.RowSpan = 2;//所跨的欄數
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);//新增自製的儲存格
            //將自製的資料列新增上去
            GridView1.Controls[0].Controls.Add(headRow);
            #endregion
            
            #region "產生多重表列的第二列"
            headRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            #region "1月裡的欄位"
            //用法都與第一列相同
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "2月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "3月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "4月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "5月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "6月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "7月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "8月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "9月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "10月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "11月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "12月裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #region "全年裡的欄位"
            head2Cell = new TableCell();
            head2Cell.Text = "核卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "開卡";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            head2Cell = new TableCell();
            head2Cell.Text = "動用";
            head2Cell.BackColor = Color.LightSteelBlue;
            head2Cell.HorizontalAlign = HorizontalAlign.Center;
            head2Cell.Wrap = false; 
            headRow.Cells.Add(head2Cell);
            #endregion
            #endregion
            GridView1.Controls[0].Controls.Add(headRow);
        #endregion
        }
    }