Problem with ComboBox DataBinding to ROLBProblem with ComboBox DataBinding to ROLB
Old forum URL: forums.lhotka.net/forums/t/6038.aspx
lukky posted on Monday, December 22, 2008
Hi,
I have CustomerInfo and CustomerInfoList classes deriving from ReadOnlyBase and ReadOnlyListBase.
On a Windows Form, I create a BindingSource, and set its DataSource to the CustomerInfo class (I have previously added the CustomerInfo class as a DataSource).
Then I set the ComboBox DataSource to the BindingSource, set the DisplayMember to CustName, and the ValueMember to CustId.
Finally, I set the ComboBox DataBinding SelectedValue to point to the yet another BindingSource that tracks the currently selected Invoice (BusinessBase derived).
This is a typical ComboBox databinding scenario, but for some reason it doesn't work as expected.
When I select a CustName from the ComboBox, the CustId doesn't get pushed to the Invoice.
So I set a temporary event handler on the SelectedIndexChanged to examine the currently selected CustomerInfo. I assume that since I have set the ValueMember of my ComboBox to CustId, whenever the ComboBox_SelectedIndexChanged event is raised, the ComboBox.SelectedValue should be the CustId. But I'm seeing the actual CustomerInfo object. This seems to prevent the DataBinding mechanism to work, as it is expecting an Integer (CustId), not a CustomerInfo.
Am I missing something basic here ? Should this scenario work as described ?
Thank you.
ajj3085 replied on Monday, December 22, 2008
I think you'll want your BindingSource.DataSource to be set to the CustomerInfoList class.
lukky replied on Monday, December 22, 2008
Hi,
Actually, at runtime, this is what I do. But, at design time, in order for the Visual Studio design time data binding stuff to work, one has to set the BindingSource's DataSource to the "Type" of objects that will be exposed by the BindingSource, so in my case, that's the CustomerInfo class. This allows me to select the ValueMember and DisplayMember for the ComboBox.
Then at runtime, I obtain a list of CustomerInfo object by calling the factory method CustomerInfoList.GetList(), then I set the BindingSource's DataSource property to the list.
Regards.
lukky replied on Monday, December 22, 2008
Hi again,
Actually, I have tried setting the BindingSource's DataSource at design time to the CustomerInfoList, and what have you, it works....
I had always used List<T> before, and it would work by setting the BindingSource's DataSource to the type within the List<T>. But with ROLB/ROB, things seem to be different, and one has to use ROLB at design time. I'd like to understand the difference in behavior.
Now, I do have another issue.
Normally, when you set the DataSourceUpdateMode to OnPropertyChanged, the value is pushed as soon as the user selects an entry in the ComboBox. At least this is what I see when I use List<T>. Using ROLB, the user needs to tab off the ComboBox in order for the value to be pushed. I have added a handler to the Binding.Format event, and it fires only after user tabs off.
Strange. Any one can shed some light on this ?
Thanks again.
ajj3085 replied on Monday, December 22, 2008
I think that when the BindingSource's Type changes, it loses other settings as well, which could be why you were getting the "strange" behavior.
I would double check the update mode on the combos' Value property; again it's possible it "forgot" it's setting when you changed the BindingSource in design time.
Copyright (c) Marimer LLC