Detecting selection change in ComboBox column of a DataGridView

D

David Jackson

Hello,

I have an unbound DataGridView of which one of the columns is a ComboBox
colum containing category data, plus an additional option called <new> So
when the ComboBox is dropped down it looks like this

<new>
First
Second
Third
Fourth

I would like to add an event to this ComboBox so that I can detect if the
user chooses <new> or not. I have tried various combinations of the very
rich DataGridView event model e.g. the CellValueChanged event. The problem
with this is that it doesn't fire when the ComboBox itself changes - it only
fires when the contents of the underlying cell changes i.e. when the user
moves to another cell.

I found this via Google
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=54048&SiteID=1

but I can't get it to work. It seems to have been written using a beta
version of .NET2 so maybe things were changed in the released version.

I'm wondering if what I'm trying to achieve is even possible? Is it possible
to grab the ComboBox in a ComboBox column and wire up new events for it?

Thanks in advance.

DJ
 
T

Tim Van Wassenhove

David Jackson schreef:
Hello,

I have an unbound DataGridView of which one of the columns is a ComboBox
colum containing category data, plus an additional option called <new> So
when the ComboBox is dropped down it looks like this

<new>
First
Second
Third
Fourth

I would like to add an event to this ComboBox so that I can detect if the
user chooses <new> or not. I have tried various combinations of the very
rich DataGridView event model e.g. the CellValueChanged event. The problem
with this is that it doesn't fire when the ComboBox itself changes - it only
fires when the contents of the underlying cell changes i.e. when the user
moves to another cell.

Subscribe to the EditingControlShowing event.. Verify if the control is
a ComboBox and if it is, subscribe to it's SelectionChangeCommitted event:

ComboBox cbo = e.Control as ComboBox;
if (cbo != null) {
cbo.SelectionChangeCommitted += ...;
}


In that handler you simply write the values back to the binding:
foreach(Binding binding in comboBox.DataBindings) {
binding.WriteValue();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top