业务需要,动态生成表,同一列中数据相同的单元格需要合并。
解决方案,创建Table控件处理类,代码如下:
复制代码 代码如下:
/// <summary>表格控件相关操作类
/// </summary>
public static class aspTable
{
/// <summary>合并行
/// </summary>
/// <remarks>版权信息:,, 2013-06-21 14:20:36</remarks>
/// <param>Table</param>
/// <param>起始行</param>
/// <param>结束行</param>
/// <param>要合并的列索引</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>版权信息:,,</SPAN><SPAN> 2013-06-21 15:24:34</remarks></SPAN>
/// <param>Table</param>
/// <param>起始行</param>
/// <param>结束行</param>
/// <param>要合并的列索引</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]);
}
}
}
新闻热点
疑难解答
图片精选