C# – How to deleselect / blank a databound ComboBox? SelectedIndex = -1 does not work

ccomboboxnetwinforms

I am trying to deselect (blank out) a number of combo-boxes in my windows forms application. In my application I have a Reset method that sets the SelectedIndex for each combo to -1. All of my combo-boxes are databound, i.e. each combo-box is populated using a datasource.

I have noticed that sometimes my Reset method works, i.e. it deselects the currently selected item and blanks the combo. However, other times it chooses the first item (SelectedIndex = 0) straight after I attempt to set it to -1. From a users point of view this looks like a bug as it doesn't always "clear" the form.

According to MSDN:

"To deselect the currently selected item, set the SelectedIndex to -1. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item."

Does anyone know of a work around?

Many thanks

Best Answer

Use combination of the void and property

comboBox.ResetText();

 //to reset selected value
comboBox.SelectedIndex = -1;
Related Topic