0
votes

I have developed a small application but ran into this problem, so I'm starting with a clean slate to try and isolate it. So far, I made a very simple Windows form (VS 2017) application connecting to an SQL data table. On the form, I have a combo box for selecting a table row that is bound to the data set. I have it displaying an order number, and have its value member as an EntryID number as shown, which is also the "selected value".

Combo Box Data Settings

I also have a simple text box on the form displaying the EntryID.

When I run the app, the combo box initially displays an ordered list of order numbers like this:

enter image description here

Before selecting an item, if I scroll through the list using the form's tool bar selector, the EntryID text box value corresponds to the combo box value, so Ord40 selects the last data set row (where EntryID = 1003).

When an item is selected, however, the combo box list order changes. For example, after a few selections, I get:

enter image description here

But if I select the last display item, "Ord 20", I still get the data row EntryID = 1003. In other words, although the bound data set does not change, the combobox scrambles the displayed item. Put yet another way, if the combobox has a set of display fields and a corresponding set of value fields, the display field text get out of sync with the underlying value. I hope that makes sense. This is straight, out of the box, no altered code on a fresh project, and I have tried different settings on the "selected value" property. Any help would be much appreciated. James

1

1 Answers

2
votes

You are binding SelectedValue of the ComboBox to the same data source which you use as data source of the control. Setting DataSource is just for showing items in dropdown. Setting SelecetdValue if for changing the value of bound property/column.

You never want to bind SelectedValue to the same data source which you use for DataSource.

Just as a an example, assuming you have the following tables:

  • Product (Id, Name, CategoryId, Price)
  • Category (Id, Name)

Then if you want to show a ComboBox for CategoryId in the edit form of Product, the setup of the categoryIdComboBox should be:

  • DataSource: categoriesDataSource
  • DisplayMember: Name
  • ValueMember: Id
  • SelectedValue: productsBindigSource - CategoryId