简体   繁体   中英

Check Two DataGridViews if both of them are equal or not in c# windows form

I have two datagridviews. I want to check if both of them are same or not, the columns are always the same. I just want to check if the rows are same or not.

There may be better ways, but ...

    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());

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM