forum

Home / DeveloperSection / Forums / Compare 2 datagridview's

Compare 2 datagridview's

Erik Pasmar253813-Feb-2014
Hi. I have a problem with compareing. i made code to compare if values are same copy them to datagridview3. But now i need to copy them to datagridview4 if they are not same (  datagridview1 != datagridview2)  

//pobarvaj
            var style1 = this.dataGridView1.DefaultCellStyle.Clone();
            style1.BackColor = Color.Green;
            var style2 = this.dataGridView2.DefaultCellStyle.Clone();
            style2.BackColor = Color.Blue;
            //primerjaj
       
            var duplicateTable = (dataGridView2.DataSource as DataTable).Clone();

            // iterating the DataGridView rows to get the right order
            foreach (DataGridViewRow viewRow1 in this.dataGridView1.Rows)
            {
                bool isDuplicate = false;
                DataRowView row1 = viewRow1.DataBoundItem as DataRowView;
                if (row1 != null && viewRow1.DefaultCellStyle != style1)
                {
                    foreach (DataGridViewRow viewRow2 in this.dataGridView2.Rows)
                    {
                        DataRowView row2 = viewRow2.DataBoundItem as DataRowView;
                        
                        if (row2 != null && row1.Row["cel1"].Equals(row2.Row["cel2"]))
                   
                        {
                           
                            if (viewRow2.DefaultCellStyle != style2)
                            {
                                
                                duplicateTable.Rows.Add(row2.Row.ItemArray);
                            }

                            isDuplicate = true;
                            viewRow2.DefaultCellStyle = style2;
                        }
                    }
                }
                if (isDuplicate)
                {
                    viewRow1.DefaultCellStyle = style1;
                    this.dataGridView3.DataSource = duplicateTable;
                }
            }

Updated on 16-Feb-2014

Can you answer this question?


Answer

1 Answers

Liked By