2

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?

JonF
  • 2,336
  • 6
  • 38
  • 57
  • How about the cell click event? – Nigel B May 31 '12 at 14:28
  • As far as I know combobox will fire SelectedIndexChanged event even if the item/index isn't changed at all. You can store your current selection somewhere and then compare it with user's choice. – Blablablaster May 31 '12 at 14:35
  • get the old cell value into {if cellvalue is number copy into an int/short/byte variable} [if cellvalue is string copy it into a label] something you can use in your code and then compare old one and new one – sihirbazzz Jun 03 '12 at 01:25

2 Answers2

2

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.

David Hall
  • 32,624
  • 10
  • 90
  • 127
0

try to see with event : - DropDown - DropDownClosed

LittleJC
  • 133
  • 1
  • 5