首页
最近更新
热门教程
ASP.NET
开发语言
开发环境
AJax教程
控件开发
统计报表
数据库
Web服务
安装部署
HTML教程
Javascript
XML教程
Community Server
NHibernate
书籍推荐
常用工具
实用代码
教程全文搜索
首页
>>
vs2005的知识库
>>
扩展GridView控件(11) - 合并指定列的相邻且内容相同的单元格
添加人:
vs2005
添加时间:2007-09-04 阅读次数:3186
收藏此教程
介绍
扩展GridView控件:
合并指定列的相邻且内容相同的单元格
使用方法(设置属性):
MergeCells - 需要合并单元格的列的索引(用逗号“,”分隔)
关键代码
实现“合并指定列的相邻且内容相同的单元格”功能的代码
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
using
System.Web.UI.WebControls;
6
using
System.Web.UI;
7
8
namespace
YYControls.Helper
9
{
10
/**/
/**/
/**/
///
<summary>
11
///
SmartGridView的Helper
12
///
</summary>
13
public
class
SmartGridView
14
{
15
/**/
/**/
/**/
///
<summary>
16
///
合并指定列的相邻且内容相同的单元格
17
///
</summary>
18
///
<param name="gv">
GridView
</param>
19
///
<param name="columnIndices">
需要合并单元格的列的索引(用逗号“,”分隔)
</param>
20
public
static
void
MergeCells(GridView gv,
int
[] columnIndices)
21
{
22
//
指定的列中需要设置RowSpan的单元格的行索引
23
int
[] aryInt
=
new
int
[columnIndices.Length];
24
//
是否重新指定aryInt的相关元素的值
25
//
aryInt中的元素与aryBln中的元素一一对应
26
bool
[] aryBln
=
new
bool
[columnIndices.Length];
27
//
aryInt初值均为0
28
for
(
int
i
=
0
; i
<
aryInt.Length; i
++
)
29
{
30
aryInt[i]
=
0
;
31
}
32
//
aryBln初值均为true
33
for
(
int
i
=
0
; i
<
aryBln.Length; i
++
)
34
{
35
aryBln[i]
=
true
;
36
}
37
38
for
(
int
i
=
1
; i
<
gv.Rows.Count; i
++
)
39
{
40
//
本行和上一行均为DataControlRowType.DataRow
41
if
(gv.Rows[i].RowType
==
DataControlRowType.DataRow
&&
gv.Rows[i
-
1
].RowType
==
DataControlRowType.DataRow)
42
{
43
//
遍历指定的列索引
44
for
(
int
j
=
0
; j
<
columnIndices.Length; j
++
)
45
{
46
//
列索引超出范围则不处理
47
if
(columnIndices[j]
<
0
||
columnIndices[j]
>
gv.Columns.Count
-
1
)
continue
;
48
49
//
相邻单元格的内容相同
50
if
(gv.Rows[i].Cells[columnIndices[j]].Text
==
gv.Rows[i
-
1
].Cells[columnIndices[j]].Text)
51
{
52
if
(aryBln[j])
53
aryInt[j]
=
i
-
1
;
54
55
if
(gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan
==
0
)
56
gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan
=
1
;
57
58
gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan
++
;
59
gv.Rows[i].Cells[columnIndices[j]].Visible
=
false
;
60
61
aryBln[j]
=
false
;
62
}
63
else
64
{
65
aryBln[j]
=
true
;
66
}
67
}
68
}
69
}
70
}
71
}
72
}
上面的MergeCells(GridView gv, int[] columnIndices)方法用于实现“合并指定列的相邻且内容相同的单元格”,第一个参数是GridView,第二个参数是需要合并单元格的列的索引(用逗号“,”分隔)。
为GridView新增一个属性
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
using
System.ComponentModel;
6
7
namespace
YYControls
8
{
9
/**/
/**/
/**/
///
<summary>
10
///
SmartGridView类的属性部分
11
///
</summary>
12
public
partial
class
SmartGridView
13
{
14
private
string
_mergeCells;
15
/**/
/**/
/**/
///
<summary>
16
///
需要合并单元格的列的索引(用逗号“,”分隔)
17
///
</summary>
18
[
19
Browsable(
true
),
20
Description(
"
需要合并单元格的列的索引(用逗号“,”分隔)
"
),
21
Category(
"
扩展
"
)
22
]
23
public
virtual
string
MergeCells
24
{
25
get
{
return
_mergeCells; }
26
set
{ _mergeCells
=
value; }
27
}
28
}
29
}
继承YYControls.SmartGridViewFunction.ExtendFunction抽象类,重写其Execute()方法
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
using
System.Web.UI.WebControls;
6
7
namespace
YYControls.SmartGridViewFunction
8
{
9
/**/
/**/
/**/
///
<summary>
10
///
扩展功能:合并指定列的相邻且内容相同的单元格
11
///
</summary>
12
public
class
MergeCellsFunction : ExtendFunction
13
{
14
/**/
/**/
/**/
///
<summary>
15
///
构造函数
16
///
</summary>
17
public
MergeCellsFunction()
18
:
base
()
19
{
20
21
}
22
23
/**/
/**/
/**/
///
<summary>
24
///
构造函数
25
///
</summary>
26
///
<param name="sgv">
SmartGridView对象
</param>
27
public
MergeCellsFunction(SmartGridView sgv)
28
:
base
(sgv)
29
{
30
31
}
32
33
/**/
/**/
/**/
///
<summary>
34
///
扩展功能的实现
35
///
</summary>
36
protected
override
void
Execute()
37
{
38
this
._sgv.DataBound
+=
new
EventHandler(_sgv_DataBound);
39
}
40
41
/**/
/**/
/**/
///
<summary>
42
///
SmartGridView的DataBound事件
43
///
</summary>
44
///
<param name="sender"></param>
45
///
<param name="e"></param>
46
void
_sgv_DataBound(
object
sender, EventArgs e)
47
{
48
string
[] ary
=
this
._sgv.MergeCells.Split(
'
,
'
);
49
int
[] columnIndices
=
new
int
[ary.Length];
50
51
//
将字符串数组转为整型数组
52
for
(
int
i
=
0
; i
<
columnIndices.Length; i
++
)
53
{
54
int
j;
55
if
(
!
Int32.TryParse(ary[i],
out
j))
56
{
57
//
转整型失败则赋值为-1,“合并指定列的相邻且内容相同的单元格”则不会处理
58
j
=
-
1
;
59
}
60
61
columnIndices[i]
=
j;
62
}
63
64
YYControls.Helper.SmartGridView.MergeCells(
this
._sgv, columnIndices);
65
}
66
}
67
}
68
OK
源码下载
共
1
页 第
1
页
上一页
1
下一页
相关的教程:
控件开发
GridView
扩展GridView控件
收藏此教程
Currently.-0.05/5
-5
-4
-3
-2
-1
0
1
2
3
4
5
当前平均分:
0.0
(
2
次打分)
-5
-4
-3
-2
-1
0
1
2
3
4
5
推荐阅读
GridView 72般绝技(一)
GridView 72般绝技(三)
GridView 72般绝技(五)
GridView 72般绝技(二)
GridView 72般绝技(四)
在GridView显示时间列时,设置时间的格式
C#中加强ListView控件的功能
用ASP.NET AJAX框架扩展HTML Map控件
利用窗体的自定义属性实现窗体传值
GridView 72般绝技(五)
添加评论
评论主题
您的大名
您的评论
验证码
评论列表
ASP.NET论坛
|
网站帮助
|
加入收藏
知识库搜索:
用户信息
欢迎您,游客。
登录
|
注册
为什么要注册?
马上加入GotAspx,建立自己的知识库,与大家分享您的知识库,还可获得丰厚积分奖励!
最新文章
使用C#调用外部Ping命令获取网络连接情况
实战HTTP Handler (6) -- 条码随意打
实战HTTP Handler (5) -- 不用临时文件,直接打开动态生成的文件
实战HTTP Handler (3) -- 动态生成图片
实战HTTP Handler (4) -- 与Web程序共享Session
实战HTTP Handler (2) -- 向HTTP 处理程序传递参数
实战HTTP Handler (1) -- 创建一个最简单的HTTP Handler
对DataGrid的基础操作总结
热门文章
使用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图
实战HTTP Handler (1) -- 创建一个最简单的HTTP Handler
扩展GridView控件(11) - 合并指定列的相邻且内容相同的单元格
实战HTTP Handler (6) -- 条码随意打
查询IP所在区段
关于 NHibernate 连接 Access 小结
对DataGrid的基础操作总结
获得字符串长度(中文字符占2)