扩展GridView控件:
合并指定列的相邻且内容相同的单元格
使用方法(设置属性):
MergeCells - 需要合并单元格的列的索引(用逗号“,”分隔)
关键代码
实现“合并指定列的相邻且内容相同的单元格”功能的代码
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using System.Web.UI.WebControls;
6using System.Web.UI;
7
8namespace 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新增一个属性
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using System.ComponentModel;
6
7namespace 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()方法
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using System.Web.UI.WebControls;
6
7namespace 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}
浙公网安备 33010602011771号