C# Windows.Form ComboBox 无法设置未绑定控件的 SelectedValue 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/983350/
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-06 04:53:08  来源:igfitidea点击:

Windows.Form ComboBox Cannot set the SelectedValue Property of Unbound Control

c#combobox

提问by Amar Patel

I cannot set the default combobox selected value for an unbound combobox. Here is my code:

我无法为未绑定的组合框设置默认的组合框选定值。这是我的代码:

         System.Console.WriteLine("Current Tag Org Id = " + CurrentTag.Org.OrgId);
         ddlRUC.SelectedValue = CurrentTag.Org.OrgId;
         System.Console.WriteLine("ddlRUC selected value = " + ddlRUC.SelectedValue);

Here is the output: Current Tag Org Id = 285 ddlRUC selected value =

这是输出:当前标签组织 Id = 285 ddlRUC 选择的值 =

Note that ddlRUC.SelectedValue has not been set to 285. Does the datasource need to be bound in order to use the SelectedValue property? If so, how do I set the default item shown in a combobox that is not bound?

请注意,ddlRUC.SelectedValue 尚未设置为 285。是否需要绑定数据源才能使用 SelectedValue 属性?如果是这样,如何设置未绑定的组合框中显示的默认项目?

采纳答案by Scott Ewers

The SelectedValue property will only work for a databound listbox. If you can create your list items in a List<>, you can then bind the list to the control and SelectedValue will work as you would like.

SelectedValue 属性仅适用于数据绑定列表框。如果您可以在 List<> 中创建列表项,那么您可以将列表绑定到控件,而 SelectedValue 将按您的意愿工作。

回答by atfergs

Does your unbound ComboBox have 286 items in it? Otherwise that id won't mean anything to it.

您未绑定的 ComboBox 中是否有 286 个项目?否则那个 id 对它没有任何意义。

回答by AllenG

I may be misunderstanding exactly what you're trying to accomplish, but the ComboBox either has to have Items (I believe it has .Items, just like a ListBox) or it has to be bound to a datasource before there can be a default item.

我可能误解了您要完成的任务,但是 ComboBox 要么必须具有 Items(我相信它具有 .Items,就像 ListBox 一样),要么必须先绑定到数据源,然后才能有默认项.

回答by Matthew Jones

Do the items in your combobox have values? You could use Items.FindByText(string text) or Items.FindByValue(string value) to return the ListItem you are looking for.

您的组合框中的项目有值吗?您可以使用 Items.FindByText(string text) 或 Items.FindByValue(string value) 返回您要查找的 ListItem。

回答by Fredrik M?rk

The documentation for SelectedValuestates that the property will return "an object containing the value of the member of the data source specified by the ValueMember property". The ValueMemberproperty documentation states that it represents the name of an object property in the collection that is assigned to the DataSource property.

SelectedValue 的文档指出,该属性将返回“一个包含由 ValueMember 属性指定的数据源成员的值的对象”。该ValueMember属性文档指出它代表分配给DataSource属性的集合中的对象属性的名称。

So yes, ValueMember works only together with a databound data source.

所以是的,ValueMember 只能与数据绑定数据源一起使用。

回答by Henk Holterman

A combobox (like a Listbox) has 2 mechanisms for dealing with the selection. Either:

组合框(如列表框)有两种处理选择的机制。任何一个:

  1. You assign a List to the DataSource property and set the ValueMember and DisplayMember to the names of properties of items of that list. Or,

  2. You fill the Items property with objects of your choice, the ToString()will be displayed.

  1. 您将一个 List 分配给 DataSource 属性,并将 ValueMember 和 DisplayMember 设置为该列表项的属性名称。或者,

  2. 您用您选择的对象填充 Items 属性,ToString()将显示 。

In scenario 1) you can use SelectedValue to get/set the selection based on the ValueMember.

在场景 1) 中,您可以使用 SelectedValue 来获取/设置基于 ValueMember 的选择。

in scenario 2) you use SelectedItem property instead of SelectedValue

在场景 2) 中,您使用 SelectedItem 属性而不是 SelectedValue

So the question is, how do you fill the Items?

那么问题来了,你如何填充Items?

回答by Frank Root

I've run into this problem, wasting precious time because the value in my list was a different int type than the value I was trying to assign to the SelectedValue e.g. int32 vs int16. The thing that will drive you crazy is that there's no compile error or run time exception thrown. It just doesn't work. Get the int types to match and it works great! Note that it is essential for the combobox to be bound.

我遇到了这个问题,浪费了宝贵的时间,因为我列表中的值与我试图分配给 SelectedValue 的值是不同的 int 类型,例如 int32 与 int16。会让你发疯的事情是没有抛出编译错误或运行时异常。它只是不起作用。获取匹配的 int 类型,效果很好!请注意,绑定组合框是必不可少的。