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)
I had the same problem. I solved it by replacing the parameter "SelectedValue" with "Text" in DataBindings.Add method. MyComboBox.DataBindings.Add("Text", MyBindingSource, "MyProperty", True, DataSourceUpdateMode.OnValidation)Answer2:
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.Answer3:
In your validating event, if the text.isNullOrEmpty()==fallse && selectedItem==null or selectedIndex<0<ol><li>check if an item with that name already exists in the collection and change the selectedItem to that if so.</li> <li>Prompt the user for if they meant to try to create a new item.</li> <li>Do whatever you need to get the information for creating that item, wether it's displaying a form dialog, or nothing at all.</li> <li>Insert the newly created instance into the datasource the comboBox is bound to.</li> <li>Set the selectedItem property on the comboBox to the new Item.</li> </ol>