简体   繁体   English

如何在组合框类中设置所选项?

[英]How do I set selected item in a combobox class?

I have populated a combobox with list class. 我已经用list类填充了一个组合框。

cboParentMenu.DataSource = listMenu;
cboParentMenu.DisplayMember = "caption";

This is the class variables. 这是类变量。

public string caption { get; set; }
public string caption_style { get; set; }
public string cat_id { get; set; }
public int id_menu { get; set; }

Now, how do I set the selected item using the field id_menu? 现在,如何使用字段id_menu设置所选项目? I'm thinking something like this one. 我在想这样的事情。

cboParentMenu.SelectedItem.id_menu = 123;

You can do 你可以做

cboParentMenu.SelectedItem = listMenu.FirstOrDefault(x => x.id_menu == 123);

Make sure you import System.Linq . 确保导入System.Linq

尝试这个:

cboParentMenu.SelectedValue = listMenu.Find(i => i.id_menu == 123);

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

相关问题 在组合框中,如何确定突出显示的项目(未选择项目)? - In a combobox, how do I determine the highlighted item (not selected item)? 如何使用 C# 设置 comboBox 中的所选项目以匹配我的字符串? - How do I set the selected item in a comboBox to match my string using C#? C#-如何通过比较我的int值在组合框中设置所选项目? - C# - How do I set the selected item in a combobox by comparing my int value? 如何识别将选定的ComboBox的项目与一组数据相关的项目? - How do I identify the selected ComboBox's Item relating it to a set of data? 如何比较组合框中的所选项目以匹配属性中的设置类 - How do I compare the selected item in a combobox to match my settings class in the properties 如何检测ComboBox上是否未选择任何项目? - How do I detect if no selected item on ComboBox is chosen? 如何在ModelView中设置ComboBox所选项目? - How to set ComboBox selected item in ModelView? 为数据网格中的组合框设置所选项目 - Set the selected item for a combobox in a datagrid 选择特定的组合框项目后,如何将DataGridViewComboBoxCell设置为可编辑模式? - How to set DataGridViewComboBoxCell in editable mode when specific combobox item selected? 如何使用组合框选择的项目设置连接字符串? - How to set connection string by using combobox selected item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM