I'm trying to change a cell backcolor to white, when it is colored red on editting the cell. I've tried cellvaluechanged and currentcelldirtystatechanged, but the backcolor doesn't change until I leave the cell, whereas I want the cell backcolor to change as I start editing. I'm probably missing something small here, but can't seem to figure it out. The below is what I have been trying, which doesn't work as mentioned above.
Private Sub dgvdefault_cellvaluechanged(sender As Object, e As DataGridViewCellEventArgs)
If dgvdefault.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = colorfielderror Then
dgvdefault.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = colorfieldentry
End If
End Sub
Private Sub dgvdefault_currentcelldirtystatechanged(sender As Object, e As EventArgs)
If dgvdefault.CurrentCell.Style.BackColor = colorfielderror Then
dgvdefault.CurrentCell.Style.BackColor = colorfieldentry
End If
End Sub
Any help would be greatly appreciated. Thanks
You can use the DataGridView
's EditingControlShowing
event.
In C#, you would have :
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
((TextBox)e.Control).TextChanged += TextBoxCell_TextChanged;
}
private void TextBoxCell_TextChanged(object sender, EventArgs e)
{
((TextBox)sender).BackColor = Color.Red;
}