standalone combobox databinding

standalone combobox databinding

Old forum URL: forums.lhotka.net/forums/t/3534.aspx


jhw posted on Thursday, September 13, 2007

I am dealing with a single combobox instead of one that is part of a datagrid.

I need to bind it to a property of my object, but need to fill the list from another binding source. The problem is getting the combobox to have the correct intial value for the selelected item instead of just the 1st item in the binding source. There are so many combinations of settings with the databinding and the datasource that finding the right combination is not working.

Currently I can make it work manually by setting the index through code, but it would be good to make this type of binding work just thru design time binding.

Thanks

Heath

dean replied on Thursday, September 13, 2007

Are you using NameValueListBase for your list data source or something else? If so you should not need to set the value in code and this is how it should be setup:

So you have two binding sources on your form:  "formBindingSource" that represents your object that will be edited and contains the property for the combobox and a "ListBindingSource" that is the list inherited from NameValueListBase.

In the properties settings for the combobox when you edit the form, set the datasource property to "ListbindingSource", set the Text property to the "formBindingsource.property" value, set the Display Member and Value Member both to Value. (Yes, I have seen some developers use Key for value member but this is what I do).

Then in the form load event code, you set the datasource property of each of the binding sources to the correct objects.

That should do it.

Dean

jhw replied on Friday, September 14, 2007

I am using a readonly list instead of a name value list. I tried variations of what you said, but it is not working out. I was able to improve a bit from your advice but I still need to manually set the intial selectedIndex of the combobox.

 

It is only about 6 lines of code to manually set the initial index. Time to move on and quit beating my head against the screen.

 

Thanks

 

KKoteles replied on Friday, September 14, 2007

jhw,

Strange - I've never had an issue with this. 

I drag a BindingSource object on to my form and set its DataSource to my CSLA BusinessBase object.  I then go to the Data Sources window - select my BusinessBase object, expand it, change the property I am concerned about to a ComboBox and drag it on to my form. 

I add another BindingSource object and set its DataSource to my CSLA ReadOnlyListBase object.

Now I go back to the property ComboBox I just dragged on to the form, and change its DataSource value to be the name of the BindingSource object representing my CSLA ReadOnlyListBase object.

Since the ComboBox is "bound" to the BusinessBase object, the value listed in it gets set to that value; however, the dropdown itself is now all the values of the ReadOnlyListBase object.

Cheers,

Ken K

jhw replied on Friday, September 14, 2007

Ken

 

That is different than the way I have been doing it. I will give it a try.

Thanx

jhw replied on Friday, September 14, 2007

Ken

Turns out my problem was the order of setting datasources. Set the data souce for the bound object binding source last.

Copyright (c) Marimer LLC