Basit Khan

Basit Khan

  • NA
  • 336
  • 123.3k

How to compare two DataGridView Values

Mar 24 2019 5:58 AM
Hi,
 
I have two DataGridView1 and DataGridView2 which is having same columns and rows.
 
How to compare between two grids.
 
I wrote the for loop but no success.
 
below is the code.
 
For Each rw1 As DataGridViewRow In DataGridView1.Rows
For Each rw2 As DataGridViewRow In DataGridView2.Rows
If rw1.Cells(3).Value <> rw2.Cells(3).Value Then
If Not rw1.Cells(3).Value Is Nothing Then
rw1.Cells(3).Style.BackColor = Color.Red
End If
End If
Next
Next
 
Thanks
Basit

Answers (2)

0
Gajendra Jangid

Gajendra Jangid

  • 28
  • 41.2k
  • 2.2m
Mar 26 2019 12:51 AM
Hi Basit,
 
please try below code:
 
  1. for (int i = 0; i < dataGridView1.Rows.count; i++)  
  2. {  
  3.      for (int j = 0; j < dataGridView2.Rows.count; j++)  
  4.      {  
  5.    if (dataGridView1.row[i].Cells["ColName"].Value < dataGridView2.row[j].Cells["ColName"].Value)  
  6.    {  
  7.        dataGridView1.row[i].DataGridViewCellStyle.BackColor = Color.Red;  
  8.        dataGridView2.row[j].DataGridViewCellStyle.BackColor = Color.Red;  
  9.    }  
  10.   }  
  11. }  
 
0
Jignesh Kumar

Jignesh Kumar

  • 33
  • 37.9k
  • 2.8m
Mar 24 2019 6:42 AM
H Basit,
 
Please find below link,
https://stackoverflow.com/questions/22227847/compare-cells-between-two-datagridviews