Reputation: 67
I want to change the ForeColor in the cell when the text is not in the correct format, I have this code to change the color after the text is entered - is this the right way?
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
}
And how can I change it back after the user enters text in the correct format?
Upvotes: 1
Views: 42752
Reputation: 1512
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Green;
Upvotes: 0
Reputation: 600
your code seems to be correct, if you are getting the desired visual effect you should be good to go.
same way we can change the background color also..
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Green;
Upvotes: 0
Reputation: 4546
Or better use dgv_CellValidating event. If the value is not the one you want, call the code you have up there. And there you must have anoher event (dgv_CellEndEdit), where you set the ForeColor back to defualt one (so error will kinda dissapear).
Upvotes: 1
Reputation: 44595
you can set the color back to normal in the CellValidated event handler.
Upvotes: 2