I have a DataGridView
with cells containing strings. If a cell content is changed, I want to change the background
of this cell. What event is the best for doing this?
I first tried the CellValueChanged
event, but this is even called by clicking this cell without editing the content.
Here is my function code:
private void GVCrs_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
this.GVCrs.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Orange;
}
Regards R4z0R
You can try CellBeginEdit
and CellEndEdit
events,
string beforeValue = "";
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
beforeValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != beforeValue)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
beforeValue = "";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.