Hi ryutenkan,
Please check this sample. now take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.Drawing;
using System.Windows.Forms;
VB.Net
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Rows.Add("1", "Alpha");
dt.Rows.Add("1", "Beta");
dt.Rows.Add("1", "Alpha");
dt.Rows.Add("2", "Alpha");
dt.Rows.Add("2", "Beta");
dt.Rows.Add("2", "Gamma");
dt.Rows.Add("3", "Alpha");
dt.Rows.Add("3", "Beta");
dt.Rows.Add("3", "Beta");
this.dataGridView1.DataSource = dt;
for (int currentRow = 0; currentRow < dataGridView1.Rows.Count - 1; currentRow++)
{
int id = Convert.ToInt32(dataGridView1.Rows[currentRow].Cells[0].Value);
string name = dataGridView1.Rows[currentRow].Cells[1].Value.ToString();
for (int row = 0; row < dataGridView1.Rows.Count - 1; row++)
{
int idCompare = Convert.ToInt32(dataGridView1.Rows[row].Cells[0].Value);
string nameCompare = dataGridView1.Rows[row].Cells[1].Value.ToString();
if (currentRow != row)
{
if (id == idCompare && name == nameCompare)
{
dataGridView1.Rows[currentRow].DefaultCellStyle.BackColor = Color.Green;
break;
}
else
{
dataGridView1.Rows[currentRow].DefaultCellStyle.BackColor = Color.White;
}
}
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Rows.Add("1", "Alpha")
dt.Rows.Add("1", "Beta")
dt.Rows.Add("1", "Alpha")
dt.Rows.Add("2", "Alpha")
dt.Rows.Add("2", "Beta")
dt.Rows.Add("2", "Gamma")
dt.Rows.Add("3", "Alpha")
dt.Rows.Add("3", "Beta")
dt.Rows.Add("3", "Beta")
Me.dataGridView1.DataSource = dt
For currentRow As Integer = 0 To dataGridView1.Rows.Count - 1 - 1
Dim id As Integer = Convert.ToInt32(dataGridView1.Rows(currentRow).Cells(0).Value)
Dim name As String = dataGridView1.Rows(currentRow).Cells(1).Value.ToString()
For row As Integer = 0 To dataGridView1.Rows.Count - 1 - 1
Dim idCompare As Integer = Convert.ToInt32(dataGridView1.Rows(row).Cells(0).Value)
Dim nameCompare As String = dataGridView1.Rows(row).Cells(1).Value.ToString()
If currentRow <> row Then
If id = idCompare AndAlso name = nameCompare Then
dataGridView1.Rows(currentRow).DefaultCellStyle.BackColor = Color.Green
Exit For
Else
dataGridView1.Rows(currentRow).DefaultCellStyle.BackColor = Color.White
End If
End If
Next
Next
End Sub
Screenshot
![](https://i.imgur.com/kZ3aY9R.jpg)