C# 如何在combobox类中设置选定项?

C# 如何在combobox类中设置选定项?,c#,winforms,C#,Winforms,我用list类填充了一个组合框 cboParentMenu.DataSource = listMenu; cboParentMenu.DisplayMember = "caption"; 这是类变量 public string caption { get; set; } public string caption_style { get; set; } public string cat_id { get; set; } public int id_menu { get; set; } 现在

我用list类填充了一个组合框

cboParentMenu.DataSource = listMenu;
cboParentMenu.DisplayMember = "caption";
这是类变量

public string caption { get; set; }
public string caption_style { get; set; }
public string cat_id { get; set; }
public int id_menu { get; set; }
现在,如何使用field id_菜单设置所选项目?我在想类似的事情

cboParentMenu.SelectedItem.id_menu = 123;
你能行

cboParentMenu.SelectedItem = listMenu.FirstOrDefault(x => x.id_menu == 123);
确保导入
系统。Linq

尝试以下操作:

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

如果未找到任何内容,将引发异常。所以最好使用FirstOrDefault而不是Find。在这种情况下,结果将为null,然后不选择任何内容。