简体   繁体   中英

Exception when setting SelectedValue on CF Combobox

I am using Compact Framework 3.5 and have the following code:

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

Note: It works well if I use List<> of class objects as DataSource with specifying DisplayMember and ValueMember for the ComboBox.

The error is

"Cannot set the SelectedValue in a ListControl with an empty ValueMember."

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. The documentation shows the difference:

ComboBox.SelectedValue Gets or sets the value of the member property specified by the ValueMember property

ComboBox.SelectedItem Gets or sets currently selected item in the ComboBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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