C# 如何取消选择/空白数据绑定组合框?SelectedIndex = -1 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10427172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:48:47  来源:igfitidea点击:

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

c#.netwinformscombobox

提问by bobbo

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.

我试图在我的 Windows 窗体应用程序中取消选择(消隐)一些组合框。在我的应用程序中,我有一个 Reset 方法,它将每个组合的 SelectedIndex 设置为 -1。我所有的组合框都是数据绑定的,即每个组合框都使用数据源填充。

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.

我注意到有时我的重置方法有效,即它取消选择当前选定的项目并将组合空白。但是,有时它会在我尝试将其设置为 -1 后直接选择第一项(SelectedIndex = 0)。从用户的角度来看,这看起来像是一个错误,因为它并不总是“清除”表单。

According to MSDN:

根据 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."

“要取消选择当前选定的项目,请将 SelectedIndex 设置为 -1。如果该项目是数据绑定项目,则不能将 ComboBox 项目的 SelectedIndex 设置为 -1。”

Does anyone know of a work around?

有谁知道解决方法?

Many thanks

非常感谢

回答by Siti Haslina Mohd Zulkafli

you may try to use this solution..

您可以尝试使用此解决方案..

dataGrid.DataSource = Nothing

dataGrid.DataBind()

hope its help!..:D

希望它的帮助!..:D

回答by Hassan Gulzar

You can try to set the Selected Value or Item to null(Nothingin VB)

您可以尝试将 Selected Value 或 Item 设置为nullNothing在 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.

我不记得抛出异常的行为。但是,我确实记得我通常在通过数据绑定事件对组合框进行数据绑定后向组合框插入一个名为 -1 (None) 的值。我建议获取列表中的数据并将新值插入此列表。现在将组合绑定到列表。

回答by RePierre

Try assigning nullor String.Emptyto the SelectedValueproperty.

尝试将null或分配String.EmptySelectedValue属性。

回答by Sylca

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 项目来取消选择您的组合框:

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? :-)

你的组合框中的最后一个地方是空的,但它不会打扰你。会吗?:-)

回答by blb

If your target framework is 4.0 - here is the solution:

如果您的目标框架是 4.0 - 这是解决方案:

Install .Net Framework 4.5 (do not change target framework of your project, just install the framework). After installing, that line deselects databound combobox:

安装 .Net Framework 4.5(不要更改项目的目标框架,只需安装框架)。安装后,该行取消选择数据绑定组合框:

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

我的值成员是“Id”int 主键自动递增,因此该字段不包含值 0。但是,这不适用于不支持 .net45 的 Windows 版本

回答by privatejava

Try to set the [ComboBoxObj].SelectedIndex=-1; which will make it to empty value. -1 refers to deselect or nullify the value of combobox

尝试设置 [ComboBoxObj].SelectedIndex=-1; 这将使其成为空值。-1 表示取消选择或取消组合框的值

Thanks

谢谢

回答by esc

Only the following code works for me, so try:

只有以下代码对我有用,所以请尝试:

comboBox.ResetText();   //framework  4.0 

回答by Hisham

Use combination of the void and property

使用 void 和 property 的组合

comboBox.ResetText();

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

回答by Jukes

I have had this problem for a while, but if you use:

我遇到这个问题已经有一段时间了,但是如果您使用:

'ComboBox.ResetText();'

'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.

所以我最后运行该方法,一旦所有项目都在组合框中。

回答by Dil Shad

ComboBox1.SelectedItem = null;

ComboBox1.SelectedItem = null;