If it is a windows form project you can use the following:
Add int the designer the next line to subscribe to the Cell end edit event:
this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
And in the event handler you verify with you string:
private static const string yourString = "XYZ";
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
if (dataGridView.CurrentCell.Value.ToString() == yourString)
{
dataGridView.CurrentCell.Style.ForeColor = Color.Red;
}
}
You can verify for duplicates by going thru all the cells:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
for (int i = 0; i < dataGridView.ColumnCount; i++)
{
for (int j = 0; j < dataGridView.RowCount; j++)
{
if (dataGridView.Rows[j].Cells[i].Value != null)
{
if (dataGridView.Rows[j].Cells[i].Value.ToString() == dataGridView.CurrentCell.Value.ToString() && dataGridView.Rows[j].Cells[i] != dataGridView.CurrentCell)
{
dataGridView.CurrentCell.Style.ForeColor = Color.Red;
dataGridView.Rows[j].Cells[i].Style.ForeColor = Color.Red;
}
}
}
}
}