当前位置:编程学习 > asp >>

Asp.net_Table控件の单元格纵向合并

业务需要,动态生成表,同一列中数据相同的单元格需要合并。

解决方案,创建Table控件处理类,代码如下:


[plain]
/// <summary>表格控件相关操作类 
/// </summary> 
public static class aspTable 

    /// <summary>合并行 
    /// </summary> 
    /// <remarks>版权信息: 2013-06-21 14:20:36</remarks> 
    /// <param name="tbl">Table</param> 
    /// <param name="startRow">起始行</param> 
    /// <param name="endRow">结束行</param> 
    /// <param name="colIndex">要合并的列索引</param> 
    public static void SetRowSpan(Table tbl, int startRow, int endRow, int colIndex) 
    { 
        int countRowSpan = 0; 
        int spanRow = startRow; 
        string spanText = tbl.Rows[startRow].Cells[colIndex].Text; 
        for (int rowIndex = startRow; rowIndex <= endRow; rowIndex++) 
        { 
            string currentText = tbl.Rows[rowIndex].Cells[colIndex].Text; 
            //内容是否相同 
            if (currentText == spanText) 
            { 
                countRowSpan++; 
                //移除被合并的单元格 
                if (rowIndex != spanRow) 
                { 
                    tbl.Rows[rowIndex].Cells.RemoveAt(colIndex); 
                } 
            } 
            else 
            { 
                //合并 
                tbl.Rows[spanRow].Cells[colIndex].RowSpan = countRowSpan; 
                //从此行再向下比较(重置) 
                countRowSpan = 0; 
                spanRow = rowIndex--; 
                spanText = currentText; 
            } 
        } 
        //合并最后一项 
        tbl.Rows[spanRow].Cells[colIndex].RowSpan = countRowSpan; 
 
    } 
 
    /// <summary>合并行,支持多列 
    /// </summary> 
    /// <remarks><SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">版权信息:  style="FONT-FAMILY: Arial, Helvetica, sans-serif"> 2013-06-21 15:24:34</remarks></SPAN> 
    /// <param name="tbl">Table</param> 
    /// <param name="startRow">起始行</param> 
    /// <param name="endRow">结束行</param> 
    /// <param name="colIndex">要合并的列索引</param> 
    public static void SetRowSpans(Table tbl, int startRow, int endRow, params int[] colIndexs) 
    { 
        ArrayList al = new ArrayList(colIndexs); 
        al.Sort(); 
        for (int i = al.Count - 1; i >= 0; i--) 
        { 
            SetRowSpan(tbl, startRow, endRow, (int)al[i]); 
        } 
    } 

    /// <summary>表格控件相关操作类
    /// </summary>
    public static class aspTable
    {
        /// <summary>合并行
        /// </summary>
        /// <remarks>版权信息: 2013-06-21 14:20:36</remarks>
        /// <param name="tbl">Table</param>
        /// <param name="startRow">起始行</param>
        /// <param name="endRow">结束行</param>
        /// <param name="colIndex">要合并的列索引</param>
        public static void SetRowSpan(Table tbl, int startRow, int endRow, int colIndex)
        {
            int countRowSpan = 0;
            int spanRow = startRow;
            string spanText = tbl.Rows[startRow].Cells[colIndex].Text;
            for (int rowIndex = startRow; rowIndex <= endRow; rowIndex++)
            {
                string currentText = tbl.Rows[rowIndex].Cells[colIndex].Text;
                //内容是否相同
                if (currentText == spanText)
                {
                    countRowSpan++;
                    //移除被合并的单元格
                  

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,