I have a datagridview with a datagridviewcomboboxcell in a C# winform application. I am easily able to capture when a new item is selected because the CellValueChanged event fires. However, I would like to be able to detect when the combobox is opened, but the user picks the same value that was already selected. How can I capture this?

有帮助吗?

解决方案

A combination of the EditingControlShowing event and some combo box events works1.

EditingControlShowing allows us to access the embedded combo box control:

dataGridView1.EditingControlShowing += new 
    DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);


void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox control = e.Control as ComboBox;

    if (control != null)
    {            
        control.DropDown += new EventHandler(control_DropDown);
        control.DropDownClosed += new EventHandler(control_DropDownClosed);
    }
}

I've added a private class level variable to the form to store the combo box selected index.

void control_DropDown(object sender, EventArgs e)
{
    ComboBox c = sender as ComboBox;

    _currentValue = c.SelectedIndex;
}

void control_DropDownClosed(object sender, EventArgs e)
{
    ComboBox c = sender as ComboBox;
    if (c.SelectedIndex == _currentValue)
    {
        MessageBox.Show("no change");
    }
}

1. This solution fires every time the combo box is opened and closed - if you want something else (such as when the combo box commits it's change to the grid) update your question describing the exact behaviour.

其他提示

try to see with event : - DropDown - DropDownClosed

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top