天天看點

擴充GridView控件(11) - 合并指定列的相鄰且内容相同的單元格

GridView既強大又好用。為了讓它更強大、更好用,我們來寫一個繼承自GridView的控件。

<a href="http://webabcd.blog.51cto.com/1787395/345429" target="_blank">[索引頁]</a>

<a href="http://down.51cto.com/data/101204" target="_blank">[源碼下載下傳]</a>

擴充GridView控件(11) - 合并指定列的相鄰且内容相同的單元格

介紹

擴充GridView控件:

合并指定列的相鄰且内容相同的單元格

使用方法(設定屬性): 

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

關鍵代碼

實作“合并指定列的相鄰且内容相同的單元格”功能的代碼

using System; 

using System.Collections.Generic; 

using System.Text; 

using System.Web.UI.WebControls; 

using System.Web.UI; 

namespace YYControls.Helper 

        /// &lt;summary&gt; 

        /// SmartGridView的Helper 

        /// &lt;/summary&gt; 

        public class SmartGridView 

        { 

                /// &lt;summary&gt; 

                /// 合并指定列的相鄰且内容相同的單元格 

                /// &lt;/summary&gt; 

                /// &lt;param name="gv"&gt;GridView&lt;/param&gt; 

                /// &lt;param name="columnIndices"&gt;需要合并單元格的列的索引(用逗号“,”分隔)&lt;/param&gt; 

                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 &lt; aryInt.Length; i++) 

                        { 

                                aryInt[i] = 0; 

                        } 

                        // aryBln初值均為true 

                        for (int i = 0; i &lt; aryBln.Length; i++) 

                                aryBln[i] = true; 

                        for (int i = 1; i &lt; gv.Rows.Count; i++) 

                                // 本行和上一行均為DataControlRowType.DataRow 

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

                                { 

                                        // 周遊指定的列索引 

                                        for (int j = 0; j &lt; columnIndices.Length; j++) 

                                        { 

                                                // 列索引超出範圍則不處理 

                                                if (columnIndices[j] &lt; 0 || columnIndices[j] &gt; 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.ComponentModel; 

namespace YYControls 

        /// SmartGridView類的屬性部分 

        public partial class SmartGridView 

                private string _mergeCells; 

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

                [ 

                Browsable(true), 

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

                Category("擴充") 

                ] 

                public virtual string MergeCells 

                        get { return _mergeCells; } 

                        set { _mergeCells = value; } 

繼承YYControls.SmartGridViewFunction.ExtendFunction抽象類,重寫其Execute()方法

namespace YYControls.SmartGridViewFunction 

        /// 擴充功能:合并指定列的相鄰且内容相同的單元格 

        public class MergeCellsFunction : ExtendFunction 

                /// 構造函數 

                public MergeCellsFunction() 

                        : base() 

                /// &lt;param name="sgv"&gt;SmartGridView對象&lt;/param&gt; 

                public MergeCellsFunction(SmartGridView sgv) 

                        : base(sgv) 

                /// 擴充功能的實作 

                protected override void Execute() 

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

                /// SmartGridView的DataBound事件 

                /// &lt;param name="sender"&gt;&lt;/param&gt; 

                /// &lt;param name="e"&gt;&lt;/param&gt; 

                void _sgv_DataBound(object sender, EventArgs e) 

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

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

                        // 将字元串數組轉為整型數組 

                        for (int i = 0; i &lt; 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

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/345578,如需轉載請自行聯系原作者

繼續閱讀