Reputation: 89
how to compare two selected rows in a dataGridView? 1.- I need to know how to detect the two selected rows 2.- Compare the two selected rows 3.- highlight the differences "cells"
I've tried this but unfortunately I am lost.....
DataTable src1 = dataGridView1.DataSource as DataTable; //THIS IS PROBABLY NOT NEEDED
DataTable src2 = dataGridView1.DataSource as DataTable;
int index1 = 0;
for (int i = 0; i < src1.Rows.Count; i++)
{
var row1 = src1.Rows[i].ItemArray;
var row2 = src2.Rows[i].ItemArray;
for (int j = 0; j < row1.Length; j++)
{
if (!row1[j].ToString().Equals(row2[j].ToString()))
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
}
}
}
Upvotes: 0
Views: 6157
Reputation: 463
Actually your code is not good i will answer your questions :
1.- I need to know how to detect the two selected rows
dataGridView1.SelectedRows
2.- Compare the two selected rows it should look to something similar to follow :
for (int i = 0; i < dataGridView1.SelectedRows.Count-1; i++)
{
for (int j = 0; j < dataGridView1.SelectedRows.rows[i].Cells.Count; j++)
{
if(dataGridView1.SelectedRows.rows[i].Cells[j].value.Equals(dataGridView1.SelectedRows.rows[i+1].Cells[j].value))
{
dataGridView1.SelectedRows.Rows[i].Cells[j].Style.BackColor = Color.Red;
dataGridView1.SelectedRows.Rows[i+1].Cells[j].Style.BackColor = Color.Red;
}
}
}
3.- highlight the differences "cells"
dataGridView1.SelectedRows.Rows[i].Cells[j].Style.BackColor = Color.Red;
dataGridView1.SelectedRows.Rows[i+1].Cells[j].Style.BackColor = Color.Red;
Upvotes: 3