弘帝企业智能建站系统交流平台

 找回密码
 立即注册
查看: 2383|回复: 0

【C#】GridView多行标题行、改造标题行、自定义标题行完美版

[复制链接]
发表于 2020-1-5 20:26:37 | 显示全部楼层 |阅读模式
  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.         capText = new string[] { "数量", "金额", "数量", "金额", "数量", "金额", "单价", "金额" };
  17.         capWidth = new int[] { 70, 80, 70, 80, 70, 80, 70, 80 };
  18.         GV1.Controls[0].Controls.Add(CreateHeaderRow(capText, capWidth, capRowSpan, capColSpan, true));
  19.     }
  20. }
  21. /// <summary>
  22. /// =====================  自定义标题行的方法  =========================
  23. /// </summary>
  24. /// <param name="tcText">单元格Text</param>
  25. /// <param name="tcWidth">单元格宽度,为0则不设置宽度</param>
  26. /// <param name="tcRowSpan">RowSpan</param>
  27. /// <param name="tcColSpan">ColumnSpan</param>
  28. /// <param name="noSpanRow">若为True,则忽略行、列的合并Span</param>
  29. /// <returns>一行标题行</returns>
  30. private GridViewRow CreateHeaderRow(string[] tcText, int[] tcWidth, int[] tcRowSpan, int[] tcColSpan, bool noSpanRow = false)
  31. {
  32.     GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
  33.     TableHeaderCell thc;
  34.     for (int i = 0; i < tcText.Length; i++)
  35.     {
  36.         thc = new TableHeaderCell();
  37.         thc.Text = tcText[i];
  38.         if (tcWidth[i] > 0) { thc.Width = tcWidth[i]; }//若为0,则不设置width
  39.         if (!noSpanRow)
  40.         {
  41.             if (tcRowSpan[i] != 1) { thc.RowSpan = tcRowSpan[i]; }
  42.             if (tcColSpan[i] != 1) { thc.ColumnSpan = tcColSpan[i]; }
  43.         }
  44.         rowHeader.Cells.Add(thc);
  45.     }
  46.     return rowHeader;
  47. }
复制代码


摘自:https://www.cnblogs.com/xuse/p/3570688.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|弘帝企业智能建站系统 ( 皖ICP备07503252号 )

GMT+8, 2024-11-24 02:51 , Processed in 0.059629 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表