Combobox DisplayMember is showing 'key' instead of 'value'

Combobox DisplayMember is showing 'key' instead of 'value'

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


jack.doe77 posted on Monday, March 14, 2011

Hi, I'm new to CSLA (using CSLA 2 for VB)

I have Product object which one of the field is ComboBox referring to SupplierList object. When I create a new Product, initially Supplier's ComboBox is showing 'key' instead of 'value' (I can see 'value' when I clicked ComboBox). If I tried to choose a Supplier, an exception occured 'Input string was not in a correct format'.

I have tried to use SupplierList object in a form (no object binding) and it worked perfectly.

Can anyone please enlight me, what I have missed ? Thank you.

jack.doe77 replied on Monday, March 14, 2011

Anyone can help me Sad

Here's what I've done :
- create SupplierList (inherit from NameValueListBase)
- in Product's form constructor

Public Sub New(ByVal product As Product)

        InitializeComponent()

        mProduct = product
        mProduct .BeginEdit()

        Me.SupplierListBindingSource.DataSource = SupplierList.GetList
        Me.ProductBindingSource.DataSource = mProduct

        ApplyAuthorizationRules()

    End Sub

- in Product's form design, at Dealer's combobox, I set DataSource to SupplierListBindingSource, DisplayMember to Value, ValueMember to Key

Is there anything I missed ? Thank you.

JonnyBee replied on Tuesday, March 15, 2011

You probably also have a databinding on combo.Text that makes the combo show the Key initially.

Forms designer automatically adds a databinding to .Text when you add a ComboBox from the datasources window.

 

jack.doe77 replied on Tuesday, March 15, 2011

Hi Jonny,

Thanks for replying.

The ComboBox has binding to SupplierId of Product object. The ComboBox .Text property is binding to SupplierId, but has no value.

Could you please elaborate more ?

JonnyBee replied on Tuesday, March 15, 2011

The Combobox doesn't have a DataMember property - instead it has a DisplayMember and ValueMember property:

  • The DisplayMember of a Combobox gets or sets a string that specifies the property of the data source whose contents you want to display.
  • The ValueMember property determines which value gets moved into the SelectedValue of the Combo Box. In the example, whenever the user selects a Customer by "Customers.CompanyName", the SelectedValue is the "Customers.CustomerID". Whenever the SelectedValue changes, the data-binding moves the new value into the Customer object.

So you would typically setup databinding only on:

You would only want to setup databinding on combo.Text if you use the styles Simple or DropDown where the combox acts like a text box with a list of "suggested" selctions but still allow the user to enter a new text value. (And in in this case the SelctedValue will have no meaning when you add new items/string values).

 

 

jack.doe77 replied on Tuesday, March 15, 2011

Thanks a lot Jonny Smile

I thought Text property in the DataBinding as Text property. Now everything worked like a charm.

Copyright (c) Marimer LLC