I'm using Windows Forms and have a DataGridView with a DataGridViewComboBoxColumn that is bound to a data source.
When the user chooses a different item from the combo box, I'd like to immediately change the row color to indicate this new selection.
I've tested several events such as CellValueChanged and RowPrePaint, but these requires that the user clicks off the row after making the selection.
It seems like the row doesn't update immediately. Instead, it updates after the user clicks off the row. (i.e. this is how most grids work but I'd like to change this behavior and give the user immediate feedback)
You can use the EditingControlShowing
event of the DataGridView and add an event handler for the ComboBox.SelectedIndexChanged
event:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
}
}
and in the event handler, set the color for the CurrentRow
:
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
// check the selected index, update the DataGridView.CurrentRow.DefaultCellStyle.BackColor
}
}