当前位置:Gxlcms > asp.net > Asp.net_Table控件の单元格纵向合并示例

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

时间:2021-07-01 10:21:17 帮助过:30人阅读

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


/// <summary>表格控件相关操作类
/// </summary>
public static class aspTable
{
/// <summary>合并行
/// </summary>
/// <remarks>版权信息:http://www.qqextra.com,http://t.qq.com/ls_man,http://blog.csdn.net/ls_man 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">版权信息:http://www.qqextra.com,http://t.qq.com/ls_man,http://blog.csdn.net/ls_man</SPAN><SPAN 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]);
}
}
}

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

人气教程排行