views:

676

answers:

2

Winforms / .Net 3.5

I am using a combobox with the dropdownstyle set to dropdown (allows users to enter data).

The problem I am having is with how to setup the combobox so it updates my bindingsource with values from the list and also when a user enters data.

For example the combobox may contain the following values:

"Red", "White", "Blue". But the user should be able to enter "Black".

I tried using the "Text" property but the problem is my combobox display values include extra information (code and a description) than the Value (only code).

I tried using SelectedValue when adding bindings to the combobox, but then it only updates with values from the list and not when a user enters data.

MyComboBox.DataBindings.Add("SelectedValue", MyBindingSource, "MyProperty", True, DataSourceUpdateMode.OnValidation)

Any ideas?

A: 

Try changing the DropDownStyle property off of your ComboBox. Not sure if it will give you what you want but it is a new direction to look in.

viggity
it is already set to allow users type data in. the databinding is what I have issues with.
B Z
A: 

In your validating event, if the text.isNullOrEmpty()==fallse && selectedItem==null or selectedIndex<0

  1. check if an item with that name already exists in the collection and change the selectedItem to that if so.
  2. Prompt the user for if they meant to try to create a new item.
  3. Do whatever you need to get the information for creating that item, wether it's displaying a form dialog, or nothing at all.
  4. Insert the newly created instance into the datasource the comboBox is bound to.
  5. Set the selectedItem property on the comboBox to the new Item.
Maslow