简体   繁体   中英

C# Winforms - How can I dynamically set the selectedItem of combo box?

I cannot seem to figure out how to dynamically change the selected item in a combo box. I am trying this:

myComboBox.SelectedItem = item.Id;

Here item.Id is a int that corresponds to valid ValueMember that is bound to the combobox. However the combobox remains unchanged. I have trying invalidating the control after changing the selected item. What's the trick?

Thanks

尝试改用SelectedValue。

myComboBox.SelectedValue = item.Id;

You can use either of these:

ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.

Get or Set A zero-based index of the for selected item.

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