Hi ryutenkan,
Check this example. now please 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.Columns.Add("Status");
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");
for (int currentRow = 0; currentRow < dt.Rows.Count; currentRow++)
{
int id = Convert.ToInt32(dt.Rows[currentRow][0]);
string name = dt.Rows[currentRow][1].ToString();
for (int row = 0; row < dt.Rows.Count - 1; row++)
{
int idCompare = Convert.ToInt32(dt.Rows[row][0]);
string nameCompare = dt.Rows[row][1].ToString();
if (currentRow != row)
{
if (id == idCompare && name == nameCompare)
{
dt.Rows[currentRow][2] = true;
break;
}
else
{
dt.Rows[currentRow][2] = false;
}
}
else
{
dt.Rows[currentRow][2] = true;
}
}
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[2].Visible = false;
}
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[2].Value))
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Green;
}
}
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.Columns.Add("Status")
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")
For currentRow As Integer = 0 To dt.Rows.Count - 1
Dim id As Integer = Convert.ToInt32(dt.Rows(currentRow)(0))
Dim name As String = dt.Rows(currentRow)(1).ToString()
For row As Integer = 0 To dt.Rows.Count - 1 - 1
Dim idCompare As Integer = Convert.ToInt32(dt.Rows(row)(0))
Dim nameCompare As String = dt.Rows(row)(1).ToString()
If currentRow <> row Then
If id = idCompare AndAlso name = nameCompare Then
dt.Rows(currentRow)(2) = True
Exit For
Else
dt.Rows(currentRow)(2) = False
End If
Else
dt.Rows(currentRow)(2) = True
End If
Next
Next
Me.dataGridView1.DataSource = dt
Me.dataGridView1.Columns(2).Visible = False
End Sub
Private Sub dataGridView1_RowPrePaint_1(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dataGridView1.RowPrePaint
If Convert.ToBoolean(dataGridView1.Rows(e.RowIndex).Cells(2).Value) Then
dataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Green
End If
End Sub
Screenshot