Databinding to listbox
Old forum URL: forums.lhotka.net/forums/t/473.aspx
ljon posted on Monday, June 26, 2006
Hi,
I have a form that contains two list boxes they both are initially populated using NameValue list. They both have DataBinding to my Business Object to two separate Properties. The first listbox is a single mode select and this one seems to work ok. The problem I am having is on the second listbox which is multiSimple mode. What is the proper way to bind a multi select listbox to a business object?
Thanks
ajj3085 replied on Tuesday, June 27, 2006
This is similar to a question regarding CheckBoxList. I don't have an answer, but I've been thinknig about this problem too.
How do you create a list with all possible values, and each selection (or check) would add or remove that value to the list in your BO?
It seems like you'd have to build the list so that it always contained the objects, and each of those objects would have a selected property. But that also doesn't seem like good design.
Andy
Crestline replied on Tuesday, June 27, 2006
We are actually in the middle of a situation like this as well using
the CheckedBoxList control. We have it databound to a
NameValuePair object. At the moment, we may just add a method to
our object that accepts an array or something and then loop through and
make the necessary changes to the collection. Not the best
design, but not sure what else to do at this point. We then have
to have a method on the form that creates this array based on the
CheckedBoxList.
Looking forward to options others may find.
ljon replied on Tuesday, June 27, 2006
Thanks for the replies,
I actually have the my listbox DataSource pointing to a BindingSource and the BindingSource's DataSource is pointing to my NameValue list and the works fine for the initial load of the values available to the user. I have the DataBinding SelectedValue of the Listbox pointing to another BindingSource which is pointing to my Business object/property and that works as well as long as the listbox is not multiselect. My problem is I am not sure the best way (best practice) to Bind it to the object when the ListBox is a multiSelect.
Thanks again.
Crestline replied on Tuesday, June 27, 2006
... Please delete
Crestline replied on Tuesday, June 27, 2006
ljon: Thanks for the replies,
I actually have the my listbox DataSource pointing to a
BindingSource and the BindingSource's DataSource is pointing
to my NameValue list and the works fine for the initial load of the
values available to the user. I have the DataBinding SelectedValue of
the Listbox pointing to another BindingSource which is pointing to my
Business object/property and that works as well as long as the listbox
is not multiselect. My problem is I am not sure the best way (best
practice) to Bind it to the object when the ListBox is a
multiSelect.
Thanks again.
Hmm... I'm trying to get this to work and not having any luck.
I have a bindingsource filled from a NameValueList which shows the
items in the CheckedBoxList correctly and then I set the SelectedValue
of the CheckBoxList to the ID property of my bindingsource that is
hooked up to my collection object.
The NameValue list items are all listed as they should be, but nothing
is getting checked off when I hook my other binding source up to the
selected value property. I have double checked and the my
business object collection binding source does have data...
Any ideas?
GrullonM replied on Friday, January 05, 2007
I have run into the same issues where I am building a web application that has a CheckBoxList where multiple values can be selected. In addition there are a couple of ListBoxes where the SelectionMode is set to Multiple.
Has anyone found a suitable way to work with these two controls? I am pulling my hair on this.
xal replied on Sunday, January 07, 2007
The problem here is that the listbox doesn't support "binding for selected values". So you have to do that by hand.
I've seen people have the child collection contain all possible values (when there are just a few) and each child item has a property called "selected" or something similar.
Then you can bind to a listbox or checkbox and handle the selected item changed event or check changed in order to change that property accordingly.
Databinding is good, but it won't always do everything for you. Sometimes we have to actually work!! :D
Andrés
ajj3085 replied on Monday, January 08, 2007
xal: Databinding is good, but it won't always do everything for you. Sometimes we have to actually work!! :D
Boo!
Just to add to this, the object with the Selected propety that Andres suggestes building should probably be part of the UI layer, not the business layer. This UI collection would interperate adding or removing the actual BO from the BO collection as Selected is changed.
Andy
Copyright (c) Marimer LLC