您现在的位置是:网站首页> 编程资料编程资料

Asp.net_Table控件の单元格纵向合并示例_实用技巧_

2023-05-25 389人已围观

简介 Asp.net_Table控件の单元格纵向合并示例_实用技巧_

业务需要,动态生成表,同一列中数据相同的单元格需要合并。
解决方案,创建Table控件处理类,代码如下:

复制代码 代码如下:

/// 表格控件相关操作类
///

public static class aspTable
{
/// 合并行
///

/// 版权信息:http://www.qqextra.com,http://t.qq.com/ls_man,http://blog.csdn.net/ls_man 2013-06-21 14:20:36
/// Table
/// 起始行
/// 结束行
/// 要合并的列索引
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;
}
/// 合并行,支持多列
///

/// 版权信息:http://www.qqextra.com,http://t.qq.com/ls_man,http://blog.csdn.net/ls_man 2013-06-21 15:24:34
/// Table
/// 起始行
/// 结束行
/// 要合并的列索引
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]);
}
}
}

需要注意的几点,起始行一般设置为1,因为0是标题行;结束行一般设置为Table的总行数-1即可(最后一行)。

-六神源码网