如果两个DataGridViews在c#windows窗体中相同或不相同,请检查它们

时间:2017-11-06 08:25:12

标签: c# windows datagridview compare equals

我有两个datagridviews。我想检查它们是否相同,列总是相同的。我只想检查行是否相同。

1 个答案:

答案 0 :(得分:0)

可能有更好的方法,但......

    public Form1()
    {
        InitializeComponent();

        dgv1.Columns.Add(new DataGridViewTextBoxColumn());
        dgv1.Columns.Add(new DataGridViewTextBoxColumn());
        dgv1.Columns.Add(new DataGridViewTextBoxColumn());

        dgv1.Rows.Add(1, 2, 3);
        dgv1.Rows.Add(4, 5, 6);

        dgv2.Columns.Add(new DataGridViewTextBoxColumn());
        dgv2.Columns.Add(new DataGridViewTextBoxColumn());
        dgv2.Columns.Add(new DataGridViewTextBoxColumn());

        dgv2.Rows.Add(1, 2, 3);
        dgv2.Rows.Add(4, 5, 6);

        var ar1 = string.Join(",", (from row in dgv1.Rows.OfType<DataGridViewRow>()
                                    from cell in row.Cells.OfType<DataGridViewCell>()
                                    select cell.Value));

        var ar2 = string.Join(",", (from row in dgv2.Rows.OfType<DataGridViewRow>()
                                    from cell in row.Cells.OfType<DataGridViewCell>()
                                    select cell.Value));

        Debug.Print((ar1.Equals(ar2)).ToString());

    }