当前位置:IT168首页 > 技术开发 > 概述
[收藏此页] [打印] [推荐] [评论]

扩展GridView控件 - 合并指定列的相邻且内容相同的单元格

责任编辑:nancy作者:ITPUB论坛   2008-05-14   
【内容导航】
文本Tag: 微软 sql

【IT168技术文档】

  扩展GridView控件:
  合并指定列的相邻且内容相同的单元格

  使用方法(设置属性):
  MergeCells - 需要合并单元格的列的索引(用逗号“,”分隔)


  关键代码
  实现“合并指定列的相邻且内容相同的单元格”功能的代码
using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls; using System.Web.UI; namespace YYControls.Helper { /**//// <summary> /// SmartGridView的Helper /// </summary> public class SmartGridView { /**//// <summary> /// 合并指定列的相邻且内容相同的单元格 /// </summary> /// <param name="gv">GridView</param> /// <param name="columnIndices">需要合并单元格的列的索引(用逗号“,”分隔)</param> public static void MergeCells(GridView gv, int[] columnIndices) { // 指定的列中需要设置RowSpan的单元格的行索引 int[] aryInt = new int[columnIndices.Length]; // 是否重新指定aryInt的相关元素的值 // aryInt中的元素与aryBln中的元素一一对应 bool[] aryBln = new bool[columnIndices.Length]; // aryInt初值均为0 for (int i = 0; i < aryInt.Length; i++) { aryInt[i] = 0; } // aryBln初值均为true for (int i = 0; i < aryBln.Length; i++) { aryBln[i] = true; } for (int i = 1; i < gv.Rows.Count; i++) { // 本行和上一行均为DataControlRowType.DataRow if (gv.Rows[i].RowType == DataControlRowType.DataRow && gv.Rows[i - 1].RowType == DataControlRowType.DataRow) { // 遍历指定的列索引 for (int j = 0; j < columnIndices.Length; j++) { // 列索引超出范围则不处理 if (columnIndices[j] < 0 || columnIndices[j] > gv.Columns.Count - 1) continue; // 相邻单元格的内容相同 if (gv.Rows[i].Cells[columnIndices[j]].Text == gv.Rows[i - 1].Cells[columnIndices[j]].Text) { if (aryBln[j]) aryInt[j] = i - 1; if (gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan == 0) gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan = 1; gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan++; gv.Rows[i].Cells[columnIndices[j]].Visible = false; aryBln[j] = false; } else { aryBln[j] = true; } } } } } } }
上一页
1
2下一页
收藏到: 添加到“百度搜藏”添加到“QQ书签”添加到“Google书签”添加到“Yahoo收藏”添加到“和讯网摘”
【内容导航】
本文欢迎转载,转载请注明:转载自IT168 [ http://www.it168.com/ ]
本文链接:http://tech.it168.com/d/2008-05-14/200805142146171.shtml
技术开发相关文章   .net server SQL 微软
  • 暂无
友情推介