網上找了個找,最終還是自己做的比較靠譜,道理很簡單,直接看代碼

代碼:
1 /// <summary> 2 /// =================== 兩行標題行 ======================== 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void GV1_RowCreated(object sender, GridViewRowEventArgs e) 7 { 8 if (e.Row.RowType == DataControlRowType.Header) 9 { 10 e.Row.Cells.Clear(); 11 string[] capText = { "ID", "材料名稱", "規格型號", "計量單位", "入庫數量", "點驗", "發料", "庫存", "合同" }; 12 int[] capWidth = { 50, 0, 0, 0, 70, 150, 150, 150, 150 }; 13 int[] capRowSpan = { 2, 2, 2, 2, 2, 1, 1, 1, 1 }; 14 int[] capColSpan = { 1, 1, 1, 1, 1, 2, 2, 2, 2 }; 15 GV1.Controls[0].Controls.Add(CreateHeaderRow(capText, capWidth, capRowSpan, capColSpan)); 16 17 capText = new string[] { "數量", "金額", "數量", "金額", "數量", "金額", "單價", "金額" }; 18 capWidth = new int[] { 70, 80, 70, 80, 70, 80, 70, 80 }; 19 GV1.Controls[0].Controls.Add(CreateHeaderRow(capText, capWidth, capRowSpan, capColSpan, true)); 20 } 21 } 22 /// <summary> 23 /// ===================== 自定義標題行的方法 ========================= 24 /// </summary> 25 /// <param name="tcText">單元格Text</param> 26 /// <param name="tcWidth">單元格寬度,為0則不設置寬度</param> 27 /// <param name="tcRowSpan">RowSpan</param> 28 /// <param name="tcColSpan">ColumnSpan</param> 29 /// <param name="noSpanRow">若為True,則忽略行、列的合並Span</param> 30 /// <returns>一行標題行</returns> 31 private GridViewRow CreateHeaderRow(string[] tcText, int[] tcWidth, int[] tcRowSpan, int[] tcColSpan, bool noSpanRow = false) 32 { 33 GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal); 34 TableHeaderCell thc; 35 for (int i = 0; i < tcText.Length; i++) 36 { 37 thc = new TableHeaderCell(); 38 thc.Text = tcText[i]; 39 if (tcWidth[i] > 0) { thc.Width = tcWidth[i]; }//若為0,則不設置width 40 if (!noSpanRow) 41 { 42 if (tcRowSpan[i] != 1) { thc.RowSpan = tcRowSpan[i]; } 43 if (tcColSpan[i] != 1) { thc.ColumnSpan = tcColSpan[i]; } 44 } 45 rowHeader.Cells.Add(thc); 46 } 47 return rowHeader; 48 }