扩展GridView控件:
合并指定列的相邻且内容相同的单元格
使用方法(设置属性):
MergeCells - 需要合并单元格的列的索引(用逗号“,”分隔)
关键代码
实现“合并指定列的相邻且内容相同的单元格”功能的代码
Code1using 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的Helper12 /// </summary>13 public class SmartGridView14 {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初值均为028 for (int i = 0; i < aryInt.Length; i++)29 {30 aryInt[i] = 0;31 }32 // aryBln初值均为true33 for (int i = 0; i < aryBln.Length; i++)34 {35 aryBln[i] = true;36 }3738 for (int i = 1; i < gv.Rows.Count; i++)39 {40 // 本行和上一行均为DataControlRowType.DataRow41 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;4849 // 相邻单元格的内容相同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;5455 if (gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan == 0)56 gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan = 1;5758 gv.Rows[aryInt[j]].Cells[columnIndices[j]].RowSpan++;59 gv.Rows[i].Cells[columnIndices[j]].Visible = false;6061 aryBln[j] = false;62 }63 else64 {65 aryBln[j] = true;66 }67 }68 }69 }70 }71 }72}
上面的MergeCells(GridView gv, int[] columnIndices)方法用于实现“合并指定列的相邻且内容相同的单元格”,第一个参数是GridView,第二个参数是需要合并单元格的列的索引(用逗号“,”分隔)。
为GridView新增一个属性
Code1using 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 SmartGridView13 {14 private string _mergeCells;15 /**//**//**//// <summary>16 /// 需要合并单元格的列的索引(用逗号“,”分隔)17 /// </summary>18 [19 Browsable(true),20 Description("需要合并单元格的列的索引(用逗号“,”分隔)"), 21 Category("扩展")22 ]23 public virtual string MergeCells24 {25 get { return _mergeCells; }26 set { _mergeCells = value; }27 }28 }29}
继承YYControls.SmartGridViewFunction.ExtendFunction抽象类,重写其Execute()方法
Code1using 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 : ExtendFunction13 {14 /**//**//**//// <summary>15 /// 构造函数16 /// </summary>17 public MergeCellsFunction()18 : base()19 {2021 }2223 /**//**//**//// <summary>24 /// 构造函数25 /// </summary>26 /// <param name="sgv">SmartGridView对象</param>27 public MergeCellsFunction(SmartGridView sgv)28 : base(sgv)29 {30 31 }3233 /**//**//**//// <summary>34 /// 扩展功能的实现35 /// </summary>36 protected override void Execute()37 {38 this._sgv.DataBound += new EventHandler(_sgv_DataBound);39 }4041 /**//**//**//// <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];5051 // 将字符串数组转为整型数组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 }6061 columnIndices[i] = j;62 }6364 YYControls.Helper.SmartGridView.MergeCells(this._sgv, columnIndices);65 }66 }67}
来源:https://www.cnblogs.com/longshengqun/archive/2008/10/21/1316323.html