[英]How can I set the selected index of a comboBox base from its valueMember? (C# Window Form)
[英]How can I set selected in a ComboBox, based on ValueMember?
我有一个具有以下结构的组合框。 此外,我从另一个来源获取fld_id并基于该 id 我需要在 ComboBox 中选择相应的项目。 我怎样才能做到这一点?
comboBoxCustomers.DataSource = customers;
comboBoxCustomers.ValueMember = "fld_id";
comboBoxCustomers.DisplayMember = "fld_name";
例子:
列表可以包含这些项目
fld_id fld_name
65 Item1
68 Item2
69 Item3
我需要将 Item 68 设置为选中状态。
使用以下:
comboBoxCustomers.SelectedValue = fld_id(which you are getitng from another source)
我没有足够的声誉发表评论。 这个:
comboBoxCustomers.SelectedValue = fld_id
伟大工程:)但毕竟显示形式,否则就会失败。
如果您使用组合框的数据源,则可以将数据源转换回列表,找到该项目并使用该项目设置所选项目:
var stores = cbxStores.DataSource as List<store>;
var store = stores.Where(w => w.store_code == _station.store_code).FirstOrDefault();
cbxStores.SelectedItem = store;
我为自己找到的最简单的程序:
您可以在调用函数时使用参数将其绑定到某个函数中。
希望对大家有帮助:
int Idd = Convert.ToInt32(your value for the combobox you want to be selected);
for (int i = 0; i < myComboBox.Items.Count; i++)
{
myComboBox.SelectedIndex = i;
if (Convert.ToInt32( myComboBox.SelectedValue ) == Idd )
{break;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.