CSLA .NET

From Rockford Lhotka's Expert C# 2008 and VB 2008 Business Objects books

Problem with ComboBox DataBinding to ROLB

rated by 0 users
This post has 4 Replies | 0 Followers

Top 50 Contributor
Posts 132
Luc Morin Posted: Mon, Dec 22 2008 11:09 AM
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.

Luc Morin, T.P.
http://www.stlm.ca

Top 10 Contributor
Posts 3,604
Andy replied on Mon, Dec 22 2008 11:36 AM
I think you'll want your BindingSource.DataSource to be set to the CustomerInfoList class.
Top 50 Contributor
Posts 132
Luc Morin replied on Mon, Dec 22 2008 12:16 PM
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.

Luc Morin, T.P.
http://www.stlm.ca

Top 50 Contributor
Posts 132
Luc Morin replied on Mon, Dec 22 2008 1:27 PM
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.

Luc Morin, T.P.
http://www.stlm.ca

Top 10 Contributor
Posts 3,604
Andy replied on Mon, Dec 22 2008 1:53 PM
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.
Page 1 of 1 (5 items) | RSS

Please contact Magenic for your .NET consulting
and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems