Search code examples
comboboxcompact-frameworkselectedvalue

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.


Solution

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