擴展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);
                }
        }
}
 

OK
[×××] 
相關文章
相關標籤/搜索