I see I am not the first to have problems with ComboBox and will most likely not be the last

This is likely to be an easy one for most of you... i put it down to my age :)


am using Visual Studio - Visual Basic..


I have a form, datatables etc

take the Northwind sample

a table of products it contains supplier id, instead of id number i want to show supplier name
allow the user to select the supplier from the combobox.

i can do this in a grid no problem... but on detail ... the property names are not the same :(


what property fields do i set to what

Dani AI

Generated

wanted the combobox to show a supplier name but keep the underlying SupplierID; pointed out a manual AddItem approach (fine for simple static lists); asked for clarification. For bound data in Visual Studio (WinForms/VB.NET) the cleaner solution is to bind the combobox to the suppliers table and bind the combobox’s SelectedValue to the product row’s SupplierID. That way the user sees CompanyName while the form stores SupplierID.

Example (VB.NET):

' suppliersTable is a DataTable; productsBindingSource is bound to the products DataTable
comboSuppliers.DisplayMember = "CompanyName"
comboSuppliers.ValueMember   = "SupplierID"
comboSuppliers.DataSource    = suppliersTable

comboSuppliers.DataBindings.Add("SelectedValue",
                                productsBindingSource,
                                "SupplierID",
                                True,
                                DataSourceUpdateMode.OnPropertyChanged)

Notes and quick troubleshooting:

  • DisplayMember is the text users see; ValueMember is the ID stored. Bind SelectedValue (not Text) to the product field.
  • Ensure the suppliers table is populated before binding, or call BindingSource.ResetBindings(False) after fill.
  • Types must match: the SupplierID column type in suppliersTable must be compatible with the products table column; mismatched types can leave SelectedValue empty.
  • If you see a DataRowView in SelectedItem, extract the fields like DirectCast(comboSuppliers.SelectedItem, DataRowView)("CompanyName").
  • If you are actually using older VB6 (AddItem style), that is a manual approach — for DataTables in .NET prefer the DataSource/ValueMember method.

This pattern replicates the behaviour you already see working in the grid but applies it to a detail form control.

Recommended Answers

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.