Linked Questions

Popular Questions

I have a WinForms application with ComboBox in it. I want to programatically select an item depending on its value.

I tried using IndexOf but it requires the whole object, I want to do it only by value. Items are ObjectCollection and I can't use Linq on it (or don't know how).

In one place I'm setting its source like that:

private void SetItems()
{
    var items = new List<ComboItem>(3);
    //Add items to the list
    combo.BeginUpdate();
    combo.DataSource = items;
    combo.ValueMember = "Value";
    combo.DisplayMember = "Name";
    combo.EndUpdate();
}
private class ComboItem
{
    public int Value { get; set; }
    public string Name { get; set; }
}

Then (in other place) I want to set selected item depending on the value. E. g. if I had the combo values:

"Option1": 2,
"Option2": 5,
"Option3": 10

I would like to do something like:

combo.Items.SelectedValue = 5

And have in combo selected Option2. Is it even possible?

Related Questions