[英]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.