博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
扩展GridView控件(11) - 合并指定列的相邻且内容相同的单元格
阅读量:6389 次
发布时间:2019-06-23

本文共 3649 字,大约阅读时间需要 12 分钟。

GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。





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


作者:



介绍

扩展GridView控件:

合并指定列的相邻且内容相同的单元格


使用方法(设置属性): 

MergeCells -  需要合并单元格的列的索引(用逗号“,”分隔)



关键代码

实现“合并指定列的相邻且内容相同的单元格”功能的代码
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI; 

InBlock.gif 

InBlock.gif
namespace YYControls.Helper 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// SmartGridView的Helper 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class SmartGridView 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 合并指定列的相邻且内容相同的单元格 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="gv">GridView</param> 

InBlock.gif                
/// <param name="columnIndices">需要合并单元格的列的索引(用逗号“,”分隔)</param> 

InBlock.gif                
public 
static 
void MergeCells(GridView gv, 
int[] columnIndices) 

InBlock.gif                { 

InBlock.gif                        
// 指定的列中需要设置RowSpan的单元格的行索引 

InBlock.gif                        
int[] aryInt = 
new 
int[columnIndices.Length]; 

InBlock.gif                        
// 是否重新指定aryInt的相关元素的值 

InBlock.gif                        
// aryInt中的元素与aryBln中的元素一一对应 

InBlock.gif                        
bool[] aryBln = 
new 
bool[columnIndices.Length]; 

InBlock.gif                        
// aryInt初值均为0 

InBlock.gif                        
for (
int i = 0; i < aryInt.Length; i++) 

InBlock.gif                        { 

InBlock.gif                                aryInt[i] = 0; 

InBlock.gif                        } 

InBlock.gif                        
// aryBln初值均为true 

InBlock.gif                        
for (
int i = 0; i < aryBln.Length; i++) 

InBlock.gif                        { 

InBlock.gif                                aryBln[i] = 
true

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        
for (
int i = 1; i < gv.Rows.Count; i++) 

InBlock.gif                        { 

InBlock.gif                                
// 本行和上一行均为DataControlRowType.DataRow 

InBlock.gif                                
if (gv.Rows[i].RowType == DataControlRowType.DataRow && gv.Rows[i - 1].RowType == DataControlRowType.DataRow) 

InBlock.gif                                { 

InBlock.gif                                        
// 遍历指定的列索引 

InBlock.gif                                        
for (
int j = 0; j < columnIndices.Length; j++) 

InBlock.gif                                        { 

InBlock.gif                                                
// 列索引超出范围则不处理 

InBlock.gif                                                
if (columnIndices[j] < 0 || columnIndices[j] > gv.Columns.Count - 1) 
continue

InBlock.gif 

InBlock.gif                                                
// 相邻单元格的内容相同 

InBlock.gif                                                
if (gv.Rows[i].Cells[columnIndices[j]].Text == gv.Rows[i - 1].Cells[columnIndices[j]].Text) 

InBlock.gif                                                { 

InBlock.gif                                                        
if (aryBln[j]) 

InBlock.gif                                                                aryInt[j] = i - 1; 

InBlock.gif 

InBlock.gif                                                        
if (gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan == 0) 

InBlock.gif                                                                gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan = 1; 

InBlock.gif 

InBlock.gif                                                        gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan++; 

InBlock.gif                                                        gv.Rows[i].Cells[columnIndices[j]].Visible = 
false

InBlock.gif 

InBlock.gif                                                        aryBln[j] = 
false

InBlock.gif                                                } 

InBlock.gif                                                
else 

InBlock.gif                                                { 

InBlock.gif                                                        aryBln[j] = 
true

InBlock.gif                                                } 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
上面的MergeCells(GridView gv, int[] columnIndices)方法用于实现“合并指定列的相邻且内容相同的单元格”,第一个参数是GridView,第二个参数是需要合并单元格的列的索引(用逗号“,”分隔)。

为GridView新增一个属性
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ComponentModel; 

InBlock.gif 

InBlock.gif
namespace YYControls 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// SmartGridView类的属性部分 

InBlock.gif        
/// </summary> 

InBlock.gif        
public partial 
class SmartGridView 

InBlock.gif        { 

InBlock.gif                
private 
string _mergeCells; 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 需要合并单元格的列的索引(用逗号“,”分隔) 

InBlock.gif                
/// </summary> 

InBlock.gif                [ 

InBlock.gif                Browsable(
true), 

InBlock.gif                Description(
"需要合并单元格的列的索引(用逗号“,”分隔)"),    

InBlock.gif                Category(
"扩展"

InBlock.gif                ] 

InBlock.gif                
public 
virtual 
string MergeCells 

InBlock.gif                { 

InBlock.gif                        get { 
return _mergeCells; } 

InBlock.gif                        set { _mergeCells = value; } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
继承YYControls.SmartGridViewFunction.ExtendFunction抽象类,重写其Execute()方法
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif 

InBlock.gif
namespace YYControls.SmartGridViewFunction 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 扩展功能:合并指定列的相邻且内容相同的单元格 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class MergeCellsFunction : ExtendFunction 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
public MergeCellsFunction() 

InBlock.gif                        : 
base() 

InBlock.gif                { 

InBlock.gif 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="sgv">SmartGridView对象</param> 

InBlock.gif                
public MergeCellsFunction(SmartGridView sgv) 

InBlock.gif                        : 
base(sgv) 

InBlock.gif                { 

InBlock.gif         

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 扩展功能的实现 

InBlock.gif                
/// </summary> 

InBlock.gif                
protected 
override 
void Execute() 

InBlock.gif                { 

InBlock.gif                        
this._sgv.DataBound += 
new EventHandler(_sgv_DataBound); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// SmartGridView的DataBound事件 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="sender"></param> 

InBlock.gif                
/// <param name="e"></param> 

InBlock.gif                
void _sgv_DataBound(
object sender, EventArgs e) 

InBlock.gif                { 

InBlock.gif                        
string[] ary = 
this._sgv.MergeCells.Split(','); 

InBlock.gif                        
int[] columnIndices = 
new 
int[ary.Length]; 

InBlock.gif 

InBlock.gif                        
// 将字符串数组转为整型数组 

InBlock.gif                        
for (
int i = 0; i < columnIndices.Length; i++) 

InBlock.gif                        { 

InBlock.gif                                
int j; 

InBlock.gif                                
if (!Int32.TryParse(ary[i], 
out j)) 

InBlock.gif                                { 

InBlock.gif                                        
// 转整型失败则赋值为-1,“合并指定列的相邻且内容相同的单元格”则不会处理 

InBlock.gif                                        j = -1; 

InBlock.gif                                } 

InBlock.gif 

InBlock.gif                                columnIndices[i] = j; 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        YYControls.Helper.SmartGridView.MergeCells(
this._sgv, columnIndices); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 

OK
 
     本文转自webabcd 51CTO博客,原文链接:
http://blog.51cto.com/webabcd/345578
,如需转载请自行联系原作者
你可能感兴趣的文章
2018年你需要知道的13个JavaScript工具库
查看>>
当你点击按钮的时候如何设置其他按钮不可点击
查看>>
spring 高级装配
查看>>
【合集】parasoft Jtest 从安装到使用教程合集,收藏推荐!
查看>>
Python Pygame库的学习之路(1)
查看>>
信息安全与Linux系统
查看>>
Ubuntu安装mysql
查看>>
SpringCloud 微服务 (十四) 服务网关 Zuul 过滤器(Pre&Post)
查看>>
代理设计模式
查看>>
初识Shiro
查看>>
在Developerkit开发板上运行blink例程
查看>>
企业级性能、安全可靠 阿里云发布企业级大数据平台开发者版
查看>>
Spring Boot使用过程小记(一)--加载自定义的Spring XML
查看>>
Git分支关联远程GitHub分支出错
查看>>
设计模式--桥接模式
查看>>
linux笔记之进程信息查看工具
查看>>
django 自定义分页器
查看>>
Oracle命令
查看>>
转载-没有IE就没有伤害!浏览器兼容性问题解决方案汇总
查看>>
常用 tcpdump 抓包方式
查看>>