Newbie - Foreign key, combox and databinding

Newbie - Foreign key, combox and databinding

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


Michael Hildner posted on Wednesday, June 14, 2006

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

xal replied on Wednesday, June 14, 2006

Michael,
You can fill the combobox with a namevaluelist like:
cbo.DataSource=yourNVL
cbo.DisplayMember = "Value"
cbo.ValueMember = "Key"

You can then bind your bo's property to cbo.SelectedValue

Andrés

Michael Hildner replied on Wednesday, June 14, 2006

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

xal replied on Wednesday, June 14, 2006

I see. The first problem is that you dragged the Navigation control to the form directly. This isn't the actual problem, the problem is that you left all your exposed properties (in the datasources window) marked as if they should use a textbox.
What you should do is select a combobox for that specific property we're talking about. Thus, you get rid of the intermediate textbox. This will also automatically add bindings for SelectedValue.
What you can do now is remove that specific textbox, change the property to use a combobox and drag that combobox to the form.

At this point all you need to do is attach the namevaluelist to your combo box.


About your seccond issue: Do you see your property set being called? did you set a breakpoint there? Did you tab off the combobox?
How exactly are you saving your object?
.Save doesn't save the actual object, it rather returns a new object with a saved state.

We usually approach save like (I don't actually do this for every object, I have a base form that handles this for me):

Dim mClone As YourType = yourObject.Clone
Try
    mClone = mClone.Save
    yourObject = mClone
Catch
    do whatever... like show a messagebox
End

Andrés

Michael Hildner replied on Wednesday, June 14, 2006

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

xal replied on Wednesday, June 14, 2006

Oh, sorry.
I'm assumed you had that working from the previous post.

I suppose you already created a NameValueList that contains the IDs and descriptions (or names) for your allowed values (that will be foreign key). Btw, I wish you posted your object's name and the same for the property and namevaluelist. But i'll make you an example.

Suppose you have a person object. It exposes a Documentation type property.
The possible values are 1 and 2.
1 being the value for Passport and 2 for driver's license.
So you create a name value list (we'll name it "DocumentTypeNVL") that contains:
Key     Value
1         Passport
2         Driver's license


Your person object exposes a DocumentTypeId property which should be assigned either 1 or 2.

Create an instance of the name value list and bind it to your combobox like:
Dim mDocTypes as DocumentTypeNVL = DocumentTypeNVL.GetDocumentTypeNVL()

Fill your combobox like this:
cboDocType.DataSource = mDocTypes
cboDocType.DisplayMember = "Value"
cboDocType.ValueMember = "Key"

Note: This can be done with binding sources too, so you can also create a binding source for your namevaluelist and handle all this binding in the forms designer.

Now:
If you dragged the combo from the datasources list, it's SelectedValue property should already be bound to your object's DocumentTypeId property. That should be it.

I hope it's a little more clear...

Andrés

Michael Hildner replied on Wednesday, June 14, 2006

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

kdubious replied on Wednesday, June 14, 2006

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

Michael Hildner replied on Thursday, June 15, 2006

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 replied on Friday, June 16, 2006

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