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

关于gridview - 如何在GridView中合并两个单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12512771/

10-10 05:29