---
title: "Compare 2 datagridview's"  
description: "Compare 2 datagridview's"  
author: "Erik Pasmar"  
published: 2014-02-13  
updated: 2014-02-16  
canonical: https://www.mindstick.com/forum/1970/compare-2-datagridview-s  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Compare 2 datagridview's

Hi. I have a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with compareing. i made [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) to [compare](https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes) if [values](https://www.mindstick.com/forum/327/sum-textbox-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](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net)).Clone();\
// iterating the [DataGridView](https://www.mindstick.com/articles/607/working-with-datagridview-in-vc-sharp) rows to get the [right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not) [order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) [foreach](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally) (DataGridViewRow viewRow1 in this.dataGridView1.Rows) { bool isDuplicate = false; DataRowView row1 = viewRow1.DataBoundItem as DataRowView; if (row1 != [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp) && 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; } }

## Replies

### Reply by Sumit Kesarwani

Hi Erik,\
First get the datatables of both the datagridview and compare them using the following method and set the datatable as datasource upon your condition : \
\
public bool compareTbls(Datatable tbl1, Datatable tbl2){ if(tbl1.Rows.Count != tbl2.Rows.Count || tbl1.Columns.Count != tbl2.Columns.Count) return false;\
for(int i = 0; i < tbl1.Rows.Count; i++) { for(int c = 0; c < tbl.Columns.Count; c++) { if(tbl1.Rows[i][c] != tbl2.Rows.Columns[i][c]) return false; } } return true;}\
Hope this will solve your problem.


---

Original Source: https://www.mindstick.com/forum/1970/compare-2-datagridview-s

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
