简体   繁体   English

在CF Combobox上设置SelectedValue时出现异常

[英]Exception when setting SelectedValue on CF Combobox

I am using Compact Framework 3.5 and have the following code: 我使用Compact Framework 3.5并具有以下代码:

var timeouts = new[] {1, 2, 3, 4, 5};
ddlTimeout.DataSource = timeouts;
ddlTimeout.SelectedValue = 3;

And receive the following error on setting selected value. 并在设置所选值时收到以下错误。 Where's an issue? 问题出在哪里?

Cannot set the SelectedValue in a ListControl with an empty ValueMember 无法在具有空ValueMember的ListControl中设置SelectedValue

Note: It works well if I use List<> of class objects as DataSource with specifying DisplayMember and ValueMember for the ComboBox. 注意:如果我使用类对象的List <>作为DataSource,并为ComboBox指定DisplayMember和ValueMember,则效果很好。

The error is 错误是

"Cannot set the SelectedValue in a ListControl with an empty ValueMember." “无法在具有空ValueMember的ListControl中设置SelectedValue。”

Try this instead: 试试这个:

var timeouts = new[] {1, 2, 3, 4, 5};
ddlTimeout.DataSource = timeouts;
ddlTimeout.SelectedItem = 3;

You have to have the ValueMember set for SelectedValue to work. 您必须为SelectedValue设置ValueMember才能工作。 The documentation shows the difference: 文档显示了差异:

ComboBox.SelectedValue Gets or sets the value of the member property specified by the ValueMember property ComboBox.SelectedValue获取或设置ValueMember属性指定的成员属性的值

ComboBox.SelectedItem Gets or sets currently selected item in the ComboBox. ComboBox.SelectedItem获取或设置ComboBox.SelectedItem中当前选定的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM