How do I immediately change the row color when the selected index of a DataGridViewComboBox changes?

ぃ、小莉子 提交于 2020-01-17 05:16:37

问题


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)


回答1:


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
    }
}


来源:https://stackoverflow.com/questions/7800553/how-do-i-immediately-change-the-row-color-when-the-selected-index-of-a-datagridv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!