我在gridview中有一些数据,格式如下:
A B
1
2 adeel
3
4 sml
现在我想将该行与 B 列下的空单元格合并。我该怎么做?
要合并同一列中的行,您可以使用如下代码:
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 == "") ///here, chould change the term to suit other conditions, such like merging the same content of different rows in a same column.
{
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;
}
}
}
}
您可以根据需要使用“layout:coloumnSpan”或“layout:rowSpan”使对象“合并”在两列或两行上。只需将值设置为 2 即可合并超过 2 行/列。