摘要:GridView 表頭合併
剛好最近遇到對方開的需求要合併表格
參考google上的大大的範例
加入自我註解
001 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
002 ...{
003 //GridView1_RowCreated 觸發這個動作
004 if (e.Row.RowType == DataControlRowType.Header)#region
005 if (e.Row.RowType == DataControlRowType.Header)
006 ...{
007 TableCellCollection oldCell = e.Row.Cells;
008 oldCell.Clear();//將原有的表頭格式移除
009
010 //產生多重表列的第一列
011 GridViewRow headRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
012
013 //第一欄
014 TableCell head2Cell = new TableCell();
015 head2Cell.Text = "畢業學年度";
016 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black; //自訂的方法用以統一取得色彩
017 head2Cell.HorizontalAlign = HorizontalAlign.Center;
018 head2Cell.RowSpan = 2;//所跨的欄數 上下
019 headRow.Cells.Add(head2Cell);//新增自製的儲存格
020
021
022 //第二欄
023 head2Cell = new TableCell();
024 head2Cell.Text = "學士班畢業生人數";
025 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
026 head2Cell.RowSpan = 2;//所跨的欄數 上下
027 head2Cell.HorizontalAlign = HorizontalAlign.Center;
028 headRow.Cells.Add(head2Cell);//新增自製的儲存格
029
030 //第三欄
031 head2Cell = new TableCell();
032 head2Cell.Text = "完成資料調查人數";
033 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
034 head2Cell.RowSpan = 2;//所跨的欄數 上下
035 head2Cell.HorizontalAlign = HorizontalAlign.Center;
036 headRow.Cells.Add(head2Cell);//新增自製的儲存格
037
038 //第四欄
039 head2Cell = new TableCell();
040 head2Cell.Text = "完成資料調查 %";
041 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
042 head2Cell.RowSpan = 2;//所跨的欄數 上下
043 head2Cell.HorizontalAlign = HorizontalAlign.Center;
044 headRow.Cells.Add(head2Cell);//新增自製的儲存格
045
046
047
048 //第五欄
049 head2Cell = new TableCell();
050 head2Cell.Text = "畢業流向(%)";
051 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
052 head2Cell.ColumnSpan = 5;//所跨的欄數
053 head2Cell.HorizontalAlign = HorizontalAlign.Center;
054 headRow.Cells.Add(head2Cell);//新增自製的儲存格
055
056 //第六欄
057 head2Cell = new TableCell();
058 head2Cell.Text = "就業情形(%)";
059 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
060 head2Cell.ColumnSpan = 4;//所跨的欄數
061 head2Cell.HorizontalAlign = HorizontalAlign.Center;
062 headRow.Cells.Add(head2Cell);//新增自製的儲存格
063
064
065
066
067 //將自製的資料列新增上去
068 GridView1.Controls[0].Controls.Add(headRow);
069
070 //產生多重表列的第二列
071 headRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
072 #endregion
073 畢業流向裡的欄位#region 畢業流向裡的欄位
074
075 //用法都與第一列相同
076 head2Cell = new TableCell();
077 head2Cell.Text = "就業";
078 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black; //自訂的方法用以統一取得色彩
079 head2Cell.HorizontalAlign = HorizontalAlign.Center;
080 headRow.Cells.Add(head2Cell);
081
082 head2Cell = new TableCell();
083 head2Cell.Text = "升學";
084 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
085 head2Cell.HorizontalAlign = HorizontalAlign.Center;
086 headRow.Cells.Add(head2Cell);
087
088 head2Cell = new TableCell();
089 head2Cell.Text = "服役";
090 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
091 head2Cell.HorizontalAlign = HorizontalAlign.Center;
092 headRow.Cells.Add(head2Cell);
093
094 head2Cell = new TableCell();
095 head2Cell.Text = "待業";
096 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
097 head2Cell.HorizontalAlign = HorizontalAlign.Center;
098 headRow.Cells.Add(head2Cell);
099
100 head2Cell = new TableCell();
101 head2Cell.Text = "其他";
102 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
103 head2Cell.HorizontalAlign = HorizontalAlign.Center;
104 headRow.Cells.Add(head2Cell);
105
106 #endregion
107 就業情形裡的欄位#region 就業情形裡的欄位
108 head2Cell = new TableCell();
109 head2Cell.Text = "私人企業";
110 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
111 head2Cell.HorizontalAlign = HorizontalAlign.Center;
112 headRow.Cells.Add(head2Cell);
113
114 head2Cell = new TableCell();
115 head2Cell.Text = "政府部門";
116 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
117 head2Cell.HorizontalAlign = HorizontalAlign.Center;
118 headRow.Cells.Add(head2Cell);
119
120 head2Cell = new TableCell();
121 head2Cell.Text = "學校";
122 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
123 head2Cell.HorizontalAlign = HorizontalAlign.Center;
124 headRow.Cells.Add(head2Cell);
125
126 head2Cell = new TableCell();
127 head2Cell.Text = "其他";
128 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
129 head2Cell.HorizontalAlign = HorizontalAlign.Center;
130 headRow.Cells.Add(head2Cell);
131 #endregion
132 GridView1.Controls[0].Controls.Add(headRow);
133 }
134 }
002 ...{
003 //GridView1_RowCreated 觸發這個動作
004 if (e.Row.RowType == DataControlRowType.Header)#region
005 if (e.Row.RowType == DataControlRowType.Header)
006 ...{
007 TableCellCollection oldCell = e.Row.Cells;
008 oldCell.Clear();//將原有的表頭格式移除
009
010 //產生多重表列的第一列
011 GridViewRow headRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
012
013 //第一欄
014 TableCell head2Cell = new TableCell();
015 head2Cell.Text = "畢業學年度";
016 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black; //自訂的方法用以統一取得色彩
017 head2Cell.HorizontalAlign = HorizontalAlign.Center;
018 head2Cell.RowSpan = 2;//所跨的欄數 上下
019 headRow.Cells.Add(head2Cell);//新增自製的儲存格
020
021
022 //第二欄
023 head2Cell = new TableCell();
024 head2Cell.Text = "學士班畢業生人數";
025 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
026 head2Cell.RowSpan = 2;//所跨的欄數 上下
027 head2Cell.HorizontalAlign = HorizontalAlign.Center;
028 headRow.Cells.Add(head2Cell);//新增自製的儲存格
029
030 //第三欄
031 head2Cell = new TableCell();
032 head2Cell.Text = "完成資料調查人數";
033 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
034 head2Cell.RowSpan = 2;//所跨的欄數 上下
035 head2Cell.HorizontalAlign = HorizontalAlign.Center;
036 headRow.Cells.Add(head2Cell);//新增自製的儲存格
037
038 //第四欄
039 head2Cell = new TableCell();
040 head2Cell.Text = "完成資料調查 %";
041 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
042 head2Cell.RowSpan = 2;//所跨的欄數 上下
043 head2Cell.HorizontalAlign = HorizontalAlign.Center;
044 headRow.Cells.Add(head2Cell);//新增自製的儲存格
045
046
047
048 //第五欄
049 head2Cell = new TableCell();
050 head2Cell.Text = "畢業流向(%)";
051 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
052 head2Cell.ColumnSpan = 5;//所跨的欄數
053 head2Cell.HorizontalAlign = HorizontalAlign.Center;
054 headRow.Cells.Add(head2Cell);//新增自製的儲存格
055
056 //第六欄
057 head2Cell = new TableCell();
058 head2Cell.Text = "就業情形(%)";
059 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
060 head2Cell.ColumnSpan = 4;//所跨的欄數
061 head2Cell.HorizontalAlign = HorizontalAlign.Center;
062 headRow.Cells.Add(head2Cell);//新增自製的儲存格
063
064
065
066
067 //將自製的資料列新增上去
068 GridView1.Controls[0].Controls.Add(headRow);
069
070 //產生多重表列的第二列
071 headRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
072 #endregion
073 畢業流向裡的欄位#region 畢業流向裡的欄位
074
075 //用法都與第一列相同
076 head2Cell = new TableCell();
077 head2Cell.Text = "就業";
078 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black; //自訂的方法用以統一取得色彩
079 head2Cell.HorizontalAlign = HorizontalAlign.Center;
080 headRow.Cells.Add(head2Cell);
081
082 head2Cell = new TableCell();
083 head2Cell.Text = "升學";
084 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
085 head2Cell.HorizontalAlign = HorizontalAlign.Center;
086 headRow.Cells.Add(head2Cell);
087
088 head2Cell = new TableCell();
089 head2Cell.Text = "服役";
090 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
091 head2Cell.HorizontalAlign = HorizontalAlign.Center;
092 headRow.Cells.Add(head2Cell);
093
094 head2Cell = new TableCell();
095 head2Cell.Text = "待業";
096 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
097 head2Cell.HorizontalAlign = HorizontalAlign.Center;
098 headRow.Cells.Add(head2Cell);
099
100 head2Cell = new TableCell();
101 head2Cell.Text = "其他";
102 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
103 head2Cell.HorizontalAlign = HorizontalAlign.Center;
104 headRow.Cells.Add(head2Cell);
105
106 #endregion
107 就業情形裡的欄位#region 就業情形裡的欄位
108 head2Cell = new TableCell();
109 head2Cell.Text = "私人企業";
110 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
111 head2Cell.HorizontalAlign = HorizontalAlign.Center;
112 headRow.Cells.Add(head2Cell);
113
114 head2Cell = new TableCell();
115 head2Cell.Text = "政府部門";
116 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
117 head2Cell.HorizontalAlign = HorizontalAlign.Center;
118 headRow.Cells.Add(head2Cell);
119
120 head2Cell = new TableCell();
121 head2Cell.Text = "學校";
122 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
123 head2Cell.HorizontalAlign = HorizontalAlign.Center;
124 headRow.Cells.Add(head2Cell);
125
126 head2Cell = new TableCell();
127 head2Cell.Text = "其他";
128 head2Cell.BackColor = Color.Beige; head2Cell.BorderColor = Color.Black;
129 head2Cell.HorizontalAlign = HorizontalAlign.Center;
130 headRow.Cells.Add(head2Cell);
131 #endregion
132 GridView1.Controls[0].Controls.Add(headRow);
133 }
134 }
大家一起加入blogads 賺零用錢!!