问题
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
回答1:
Use combination of the void and property
comboBox.ResetText();
//to reset selected value
comboBox.SelectedIndex = -1;
回答2:
Don't know if anyone is still interested in this, seeing as it's now 5 years later, but I found a very easy workaround. Totally non-intuitive (I only found it by looking at the reference source code), but trivial to implement:
ComboBox1.FormattingEnabled = True;
Yep, that's all there is to it!
If you're curious, you can peruse the source code to see what's going on. It appears that the root cause of the bug noted by @CuppM is the attempt to set the position in the data source:
if (!FormattingEnabled || SelectedIndex != -1) {
this.DataManager.Position = this.SelectedIndex;
}
I would guess that it should have simply been '&&' instead of '||' in the condition, as the code probably shouldn't be setting the Position to an invalid value regardless of the FormattingEnabled property.
In any case, it allows for a simple workaround. And since the default behavior if the 'Format' property is blank is a no-op, you don't have to change anything else. It just works. :-)
(I should note that I have only tried this with .NET 4.7, so I can't say whether it works for prior versions of the .NET Framework.)
回答3:
You can try to set the Selected Value or Item to null (Nothing in VB)
I cant remember the behavior of throwing an exception. However, I do remember that I used to insert a value called -1, (None) to the combo-box after it was databounded usually through the databind events. I'd recommend get the data in a List and insert the new value to this list. Bind the combo to the List now.
回答4:
Only the following code works for me, so try:
comboBox.ResetText(); //framework 4.0
回答5:
ComboBox1.SelectedItem = null;
回答6:
Try assigning null or String.Empty to the SelectedValue property.
回答7:
If your target framework is 4.0 - here is the solution:
Install .Net Framework 4.5 (do not change target framework of your project, just install the framework). After installing, that line deselects databound combobox:
combobox.SelectedValue = 0;
My value member is "Id" int primary key auto-increment, so that field does not contain value 0. However, that won't work on Windows versions, that do not support .net45
回答8:
Try to set the [ComboBoxObj].SelectedIndex=-1; which will make it to empty value. -1 refers to deselect or nullify the value of combobox
Thanks
回答9:
I have had this problem for a while, but if you use:
'ComboBox.ResetText();'
it will make the text "" and leave the items in the combo box unaffected.
i used the following code in my application
private void UpdateComboBox(ComboBox Box, string Group, List<string> Numbers)
{
Box.Items.Clear();
Box.BeginUpdate();
Box.Items.Add("<<Add Contact>>");
foreach (string item in Numbers)
{
if(item != "")
Box.Items.Add(item);
}
Box.EndUpdate();
Box.ResetText();
}
So i run the method last, once all items are in the combo Box.
回答10:
Add to your combobox one empty item, something like this:
cb.Items.Add("");
After this you can deselect your combobox by selecting the last cb item:
cb.SelectedIndex = cb.Items.Count - 1;
There you go!
You'll have the last place empty in your combobox, but it wont bother you. will it? :-)
回答11:
I got the error "There is no row at position 0" when setting ComboBox.SelectedItem to -1. Replacing by ComboBox.ResetText() worked OK. This was using .Net 4.6.1, with VS 2013 where TextFormatting = True by default for ComboBoxes.
回答12:
you may try to use this solution..
dataGrid.DataSource = Nothing
dataGrid.DataBind()
hope its help!..:D
来源:https://stackoverflow.com/questions/10427172/how-to-deleselect-blank-a-databound-combobox-selectedindex-1-does-not-work