webform gridview合并单元格
gridview合并单元格
由于项目要求,需要合并某些单元格,因此特意封装了如下帮助类:

1 /// <summary> 2 /// 合并单元格 3 /// </summary> 4 public class MergeCellHelper 5 { 6 /// <summary> 7 /// 合并表头 8 /// </summary> 9 /// <param name="row">当前行</param> 10 /// <param name="startColumn">开始列</param> 11 /// <param name="endColumn">结束列</param> 12 public static void MergeHeader(GridViewRow row, int startColumn, int endColumn) 13 { 14 for (int i = startColumn; i <= endColumn; i++) 15 { 16 if (i != startColumn) 17 { 18 row.Cells[i].Visible = false; 19 continue; 20 } 21 row.Cells[startColumn].ColumnSpan = endColumn - startColumn + 1; 22 } 23 } 24 25 /// <summary> 26 /// 合并指定列的行 27 /// </summary> 28 /// <param name="gv">gridview</param> 29 /// <param name="columns">指定的列:可以多列</param> 30 public static void MergeRow(GridView gv, params int[] columns) 31 { 32 MergeRowByReferenceColumn(gv, 0, columns); 33 } 34 35 /// <summary> 36 /// 合并指定列的行(参考列一定要合理,慎用) 37 /// </summary> 38 /// <param name="gv">gridview</param> 39 /// /// <param name="referenceColumn">参照列</param> 40 /// <param name="columns">指定的列:可以多列</param> 41 public static void MergeRowByReferenceColumn(GridView gv, int referenceColumn, params int[] columns) 42 { 43 for (int i = 0; i < columns.Length; i++) 44 { 45 var currentColumn = columns[i]; 46 int rowSpan = 1; 47 for (int j = 0; j < gv.Rows.Count; j++) 48 { 49 var currentRow = gv.Rows[j]; 50 if (j < gv.Rows.Count - 1) 51 { 52 var nextRow = gv.Rows[j + 1]; 53 if (currentRow.Cells[referenceColumn].Text == nextRow.Cells[referenceColumn].Text) 54 { 55 if (currentRow.Cells[currentColumn].Text == nextRow.Cells[currentColumn].Text) 56 { 57 rowSpan = rowSpan < 2 ? 2 : rowSpan + 1; 58 nextRow.Cells[currentColumn].Visible = false; 59 } 60 else 61 { 62 gv.Rows[j - rowSpan + 1].Cells[currentColumn].RowSpan = rowSpan; 63 rowSpan = 1; 64 } 65 } 66 else 67 { 68 gv.Rows[j - rowSpan + 1].Cells[currentColumn].RowSpan = rowSpan; 69 rowSpan = 1; 70 } 71 } 72 else 73 { 74 gv.Rows[j - rowSpan + 1].Cells[currentColumn].RowSpan = rowSpan; 75 rowSpan = 1; 76 } 77 } 78 } 79 } 80 81 82 }
为了明天能幸福,今天付出再多也不后悔。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2015-04-23 mencoder mencoder 安装使用及常用参数