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

简介:
GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[源码下载]


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


作者: webabcd


介绍
扩展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
                                                } 
                                        } 
                                } 
                        } 
                } 
        } 
}
 
上面的MergeCells(GridView gv, int[] columnIndices)方法用于实现“合并指定列的相邻且内容相同的单元格”,第一个参数是GridView,第二个参数是需要合并单元格的列的索引(用逗号“,”分隔)。

为GridView新增一个属性
using System; 
using System.Collections.Generic; 
using System.Text; 
 
using System.ComponentModel; 
 
namespace YYControls 

         /// <summary> 
         /// SmartGridView类的属性部分 
         /// </summary> 
         public partial  class SmartGridView 
        { 
                 private  string _mergeCells; 
                 /// <summary> 
                 /// 需要合并单元格的列的索引(用逗号“,”分隔) 
                 /// </summary> 
                [ 
                Browsable( true), 
                Description( "需要合并单元格的列的索引(用逗号“,”分隔)"),    
                Category( "扩展"
                ] 
                 public  virtual  string MergeCells 
                { 
                        get {  return _mergeCells; } 
                        set { _mergeCells = value; } 
                } 
        } 
}
 
继承YYControls.SmartGridViewFunction.ExtendFunction抽象类,重写其Execute()方法
using System; 
using System.Collections.Generic; 
using System.Text; 
 
using System.Web.UI.WebControls; 
 
namespace YYControls.SmartGridViewFunction 

         /// <summary> 
         /// 扩展功能:合并指定列的相邻且内容相同的单元格 
         /// </summary> 
         public  class MergeCellsFunction : ExtendFunction 
        { 
                 /// <summary> 
                 /// 构造函数 
                 /// </summary> 
                 public MergeCellsFunction() 
                        :  base() 
                { 
 
                } 
 
                 /// <summary> 
                 /// 构造函数 
                 /// </summary> 
                 /// <param name="sgv">SmartGridView对象</param> 
                 public MergeCellsFunction(SmartGridView sgv) 
                        :  base(sgv) 
                { 
         
                } 
 
                 /// <summary> 
                 /// 扩展功能的实现 
                 /// </summary> 
                 protected  override  void Execute() 
                { 
                         this._sgv.DataBound +=  new EventHandler(_sgv_DataBound); 
                } 
 
                 /// <summary> 
                 /// SmartGridView的DataBound事件 
                 /// </summary> 
                 /// <param name="sender"></param> 
                 /// <param name="e"></param> 
                 void _sgv_DataBound( object sender, EventArgs e) 
                { 
                         string[] ary =  this._sgv.MergeCells.Split(','); 
                         int[] columnIndices =  new  int[ary.Length]; 
 
                         // 将字符串数组转为整型数组 
                         for ( int i = 0; i < columnIndices.Length; i++) 
                        { 
                                 int j; 
                                 if (!Int32.TryParse(ary[i],  out j)) 
                                { 
                                         // 转整型失败则赋值为-1,“合并指定列的相邻且内容相同的单元格”则不会处理 
                                        j = -1; 
                                } 
 
                                columnIndices[i] = j; 
                        } 
 
                        YYControls.Helper.SmartGridView.MergeCells( this._sgv, columnIndices); 
                } 
        } 
}
 


     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/345578 ,如需转载请自行联系原作者

相关文章
layUI数据表格可编辑表格单元格值修改之后获取修改前的值
layUI数据表格可编辑表格单元格值修改之后获取修改前的值
355 0
|
6月前
用dragstart、drag、dragend、dragover、drop、dragleave实现针对表格列的顺序进行拖拽排序(附带实现选择某几列数据显示或隐藏)
用dragstart、drag、dragend、dragover、drop、dragleave实现针对表格列的顺序进行拖拽排序(附带实现选择某几列数据显示或隐藏)
|
21天前
表格数据填充方法单元格数据填充
表格数据填充方法单元格数据填充【10月更文挑战第22天】
26 2
|
2月前
包含多列或多行的单元格
包含多列或多行的单元格。
28 1
PyQt5 技术篇-QTableWidget表格组件指定行的隐藏与显示控制实例演示,设置表格指定列的列宽方法
PyQt5 技术篇-QTableWidget表格组件指定行的隐藏与显示控制实例演示,设置表格指定列的列宽方法
697 0
PyQt5 技术篇-QTableWidget表格组件指定行的隐藏与显示控制实例演示,设置表格指定列的列宽方法
Element-ui表格的列隐藏显示会使表格头数据错乱或者表格头样式变大
Element-ui表格的列隐藏显示会使表格头数据错乱或者表格头样式变大
447 0
PyQt5 技术篇-QTableWidget表格组件的行选择与列选择实例演示,表格组件的双击事件捕获,获取表格选中单元格的值
PyQt5 技术篇-QTableWidget表格组件的行选择与列选择实例演示,表格组件的双击事件捕获,获取表格选中单元格的值
1202 0
PyQt5 技术篇-QTableWidget表格组件的行选择与列选择实例演示,表格组件的双击事件捕获,获取表格选中单元格的值
PyQt5 技术篇-设置QTableWidget表格组件默认值实例演示,如何获取QTableWidget表格组件里的值,获取表格的行数和列数
PyQt5 技术篇-设置QTableWidget表格组件默认值实例演示,如何获取QTableWidget表格组件里的值,获取表格的行数和列数
620 0
PyQt5 技术篇-设置QTableWidget表格组件默认值实例演示,如何获取QTableWidget表格组件里的值,获取表格的行数和列数