C++ datagridview动态合并单元格代码

作者:袖梨 2022-06-25

效果如下图第一列“国家”,相同的数据可以合并为一个单元格显示。
我的datagridview数据源是通过图形化界面拖放dataset与数据库教程绑定的

///


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

/// GridView
/// 第几列
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;
            }
        }
    }
}

相关文章

精彩推荐