Hi,
I'm not sure if this CSLA related or not, apologies if it's nothing CSLA specific. I did search this and the old forum.
Can someone either explain, or point me to a link that shows how to use comboboxes when binding to business objects? My main BO has a foreign key and I'd like to show the description of the FK in a combobox for the user to choose.
I think I'm thinking data-centric, but not sure how to properly set this up. Right now I'm thinking of iterating through a NameValueListBase class I have and populating the combobox like that.
Thanks,
Mike
Ok, I don't think I follow. I guess I don't know how to bind my bo's property to cbo.SelectedValue.
I created a data source that is bound to a BusinessListBase class. So this holds the collection of my BusinessBase objects I want to edit. I changed the data source from DataGridView to Details, then drug that onto the form. Now I have a bunch of labels and text boxes on my form.
I tried to do this:
this.defaultGalleryFKTextBox.DataBindings.Clear(); this.defaultGalleryFKTextBox.DataBindings.Add("Text", this.galleryNameValueListComboBox, "SelectedValue");When I change the combobox value, I can see the value in my text box change, but it's not behaving as I'd like. Specifically, it's not saving the changes when I do .Save, and it doesn't remember the values as I navigate through the child records. The second issue makes sense to me, but not sure what to do about it yet.
Do you mind explaining how to bind my bo's property to cbo.SelectedValue?
Thanks,
Mike
Andrés,
I'm laughing at myself. Maybe it's just one of those days. When you say "all you need to do is attach the namevaluelist to your combo box", what do you mean? Not sure if I should populate it manually from the NVL or if there's some data binding thing I don't get.
I understand what you're saying about the saving, thinking I should wait on that one.
Once again, thanks.
Mike
Interesting, when I drag the combo box over from the data sources, the .SelectedValue is set to "(none)" - this is under the (DataBindings) node. When I click on the down arrow to try to select something, I get a VS message box that says "Object reference not set to an instance of an object." I wonder if something is up with my VS.
I tried to add it via code like:
this
.defaultGalleryFKComboBox.DataBindings.Add("SelectedValue", employeeEditableRootListBindingSource, "DefaultGalleryFK");I guess that's the right syntax, as it runs. However, I can't tab off my combo box. It will only accept a valid Guid (which is my foreign key type), so when there's a string in there, it doesn't like it.
Do you see something obvious I'm missing? I might try to reinstall VS.
Mike
This is a painful issue, but easy to solve.
Dragging the field from the DataSources window by default binds the TEXT property of the combobox to the Property of your object. In your NameValue list situation, you 'd want to bind the SelectedValue of the combobox to the Value property of your NameValue List. In the designer, clear out the TEXT property under databindings, and set the SelectedValue property. If the designer isn't allowing that, I'd try it from scratch on a new form.
Then, use XAL's code below to populate the ComboBox with data
I assume your familiar with DisplayMember and ValueMember properties of a combobox. You set the DisplayMember equal to the string name of the property of the object in the list that is used as the ComboBoxes datasource. If you don't set this, the ComboBox calls .ToString on the item in the list. Then, the ValueMember is set to the string name of the property to use as the Value of each item.
cbo.DataSource=yourNVL
cbo.DisplayMember = "Value"
cbo.ValueMember = "Key"
Then, after the box is populated, set the BindingSource's DataSource property to the Editable business object you're binding to in the first place.
Kevin
kdubious:Dragging the field from the DataSources window by default binds the TEXT property of the combobox to the Property of your object. In your NameValue list situation, you 'd want to bind the SelectedValue of the combobox to the Value property of your NameValue List. In the designer, clear out the TEXT property under databindings, and set the SelectedValue property.
Thanks Kevin! Spot on. I searched all over the place yesterday and couldn't find anywhere that said that.
Regards,
Mike
Michael Hildner:Interesting, when I drag the combo box over from the data sources, the .SelectedValue is set to "(none)" - this is under the (DataBindings) node. When I click on the down arrow to try to select something, I get a VS message box that says "Object reference not set to an instance of an object." I wonder if something is up with my VS.
Just in case this happens to anyone else. You'll get this error if you have an invalid data source. For example, if you set up a data source off of you BO, then change the namespace of your BO. Whoops. Deleting the invalid data source fixes the problem.
Mike
Copyright (c) Marimer LLC