如何在gridview中合并两个单元格

时间:2012-09-20 12:38:55

标签: gridview merge

我在gridview中有一些数据,格式如下:

A     B
1     
2     adeel
3
4     sml

现在我想将该行与B列下的空单元格合并。我该怎么做?

2 个答案:

答案 0 :(得分:0)

要合并同一列中的行,您可以使用如下代码:

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;
            }
        }
    }
}

答案 1 :(得分:0)

您可以使用“layout:coloumnSpan”或“layout:rowSpan”根据需要使对象“合并”在两列或多行上。只需将值设置为2即可合并2行/列。