Answered by:

DataGridViewComboBoxColumn Selection Changed

Question

Hi, I would like to handle the Selection Changed event of a ComboBox column inside my datagrid, what would be the best method to do it. I tried the CellValueChanged event but that throws up an error when I am loading the datagrid. Please suggest how to do this.

Hi Mark,I did what you told and in the event handler I just wrote a code for

MessageBox.Show(CType(sender, ComboBox).SelectedIndex)

Now this is what is happening.

I click on the ComboBox column in the first row and change its value. and go to the next cell. The event does not get fired.

Now I click on the same ComboBox again, and the MessageBox comes up twice.

Then I click on the same ComboBox again, and the MessageBox comes up thrice.

And so on. Thus the problem is two fold, the event handler is not called when the Index changes and secondly, it is called multiple times. Multiple times could be because the event handler is added every time, but why isnt it getting called when the index actually changes is something I couldnt find out.

Even if I click on some other rows ComboBox column the same behaviour is repeated. Please suggest a way out, and also why is it not getting called when the index actually changes. I am using the Dec 2004 CTP build

Thanks,Kunal

[Edit] I modified the EditingControlShowing event as below. But now also the handler is getting called twice.

yes - this is the result of the editing control being reused and the fact that the combobox is databound. the SelectionChangeCommitted event might be a better event for what you are trying to do.

In newer builds this is the order when I get the messagebox for the SelectedIndexChanged event:1) I click on the ComboBox column in the first row and change its value -- I get the messagebox.2) when I goto the next cell I do not get the message.3) When I click on the same combobox again I do not get the message.4) If I select a different value in the same combobox I get the message5) If I select a different combobox on a different row I get 3 messageboxes: These are due to the cell's value being committed.

I'm going to investigate if there is something we can do for this. Thanks for the feedback!

Hey Mark,To get rid of this problem(getting messages again again!) I made one private variable of type combo box and make sure that the event register only one time. Now the message I am getting is only one time.

I think Mark has already pointed out that you should use SelectionChangeCommitted event because it insures that user has finished with the item which he wants to selects.

The annoying thing that the message is coming again and again; It is happening because EditingControlShowing event fires every time when you reach to the next row and you dare to edit the cell containing that control (combo box in this case). In order to prevent the message box from coming again and again you should first "Un-Wire" the event and then "Wire" the event. This way even though the editing control showing event get fired multiple times, we can be assured that our editing control is being wired only one time.

The difference in this example to what has previously been discussed is that we do not keep a reference to the control in a private variable. There is no need to do that, and doing so produces a lot of potential for errors. Since the DataGridView is managing the combobox controls, there may be many of these controls, and even the same "logical control" (ie, to the user, the same combo box in the same row), may get destroyed or rebuilt at any time, by the DGV. From our perspective, outside of the DataGridView, the only thing we are sure of is that the control referenced in the event args for EditingControlShowing is currently the one that the user is using...

Instead of a private reference to the combobox, we keep a private reference to the EventHandler delegate for our SelectionChangeComitted event. There's no point in creating a two new delegates every time the control is clicked.

I use this technique in my apps to control the row state conditionally based on the selections in various comboboxs. I call _dataGridView.EndEdit() in the SelectionChangeCommited event handler, to allow the other cells to react to the selection.

I believe your above solution that uses a private reference to the EventHadler delegate is a very beatiful way to solve the problem resulted in handling SelectionChangeCommitted event. I got the same problem when I tried to handle the
same event. But I can't translate your above solution in C# into VB. Can you or anybody else translate the solution for me?