当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net_Table控件の单元格纵向合并示例

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

一清专案,倒卖百万公民信息,wow天降神兵

业务需要,动态生成表,同一列中数据相同的单元格需要合并。
解决方案,创建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即可(最后一行)。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网