GridView多行标题行、改造标题行、自定义标题行完美版

网上找了个找,最终还是自己做的比较靠谱,道理很简单,直接看代码

GridView多行标题行、改造标题行、自定义标题行完美版
代码:
 
 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     }