首页 > 编程 > .NET > 正文

asp.net 合并GridView中某列相同信息的行(单元格)

2020-01-18 01:03:59
字体:
来源:转载
供稿:网友
                                        
解决方法如下(写在公共模块里,定义为静态方法方便调用):
复制代码 代码如下:

/// <summary>
/// 合并GridView中某列相同信息的行(单元格)
/// </summary>
/// <param name="GridView1">GridView</param>
/// <param name="cellNum">第几列</param>
public static void GroupRows(GridView GridView1, int cellNum)
{
int i = 0, rowSpanNum = 1;
while (i < GridView1.Rows.Count - 1)
{
GridViewRow gvr = GridView1.Rows[i];

for (++i; i < GridView1.Rows.Count; i++)
{
GridViewRow gvrNext = GridView1.Rows[i];
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
}

if (i == GridView1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}


如果多列要求合并时,可作适当修改。
一个完整的人生应该是宽恕、容忍、等待和爱!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表