NVL & Combo

NVL & Combo

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


TerryHolland posted on Tuesday, January 16, 2007

I am having a few problems with binding a NameValueList object to a combo on a winform.

Just to familiarise myself with NVL I am simply trying to bind my NVL object to my combo using the following

        cboUser.DataSource = clsUser_NVL.GetUser_NVL
        cboUser.DisplayMember = "Name"

When I run my form it the combo has populated with data (there are as many rows as there are in clsUser_NVL) but every row is displayed in the combo as

Csla.NameValueListBase`2+NameValuePair[System.Int32,System.String]

HOw do I overcome this?

Also, Im trying to work through the Project Tracker example and familiarise myselft with DataBinding. When I get to page 487, diagram 9-8 shows the Object datasource Roles with a dropdown to the right of it which should display a menu, but I dont have this.  Could anyone shed some light on this?

Thanks

Michael Hildner replied on Tuesday, January 16, 2007

Terry,

I believe I've read here it's recommended to use drag and drop.

Create a new data source from your NVL object. Change the control type to a combo box, then drag the combo box onto your form. The IDE will create a binding source and put in in the component tray. Also the .DataSource of the combo box will be set to the binding source, the .DisplayMember will be set to "Value" and the .ValueMember will be set to "Key".

I've also done it by hand, though, and honestly I'm not sure if you really need the binding source or not. Here's some code I have

// Load all galleries.
_galleries =
GalleryNameValueList.GetGalleryNameValueList();

// Set properties on the gallery combobox so it uses the
// galleries NVL (name value list).
this.defaultGalleryFKComboBox.DataSource = _galleries;
this.defaultGalleryFKComboBox.ValueMember = "Key";
this.defaultGalleryFKComboBox.DisplayMember = "Value";

// Set the binding on the combo box so the selected value gets
// put into the employee gallery FK.
this.defaultGalleryFKComboBox.DataBindings.Add("SelectedValue", this.employeeEditableRootListBindingSource, "DefaultGalleryFK");

cds replied on Tuesday, January 16, 2007

Terry, 

I believe your original problem is that you're binding the DisplayMember to the Name property whereas it should be the Value property. And as Michael points out you also need to bind the ValueMember.

Drap and drop is definitely the way to go usually as Michael says, but I remember I have had some issues with VS getting confused with multiple NVL combo boxes on the same form.

Drag and drop works for the first one, but when I try to add another combo box, populated from a different NVL it tries to reuse the same BindingSource component from the first one.

In the end, sometimes it's best to add the BindingSource component yourself and connect it to your project's ObjectDataSource then wire up the properties by hand.

HTH

Craig

TerryHolland replied on Wednesday, January 17, 2007

Thanks guys

Copyright (c) Marimer LLC