是否可以根据其值设置组合框的selectedIndex,而无需遍历数据源?
我像这样设置数据源:
caseDBTableAdapters.usersTableAdapter usersAdapter = new caseDBTableAdapters.usersTableAdapter();
caseDB.usersDataTable users;
users = usersAdapter.GetUsers();
cbOwner.DisplayMember = "fullName";
cbOwner.ValueMember = "userId";
cbOwner.DataSource = users;
必须遍历表fx似乎不那么优雅。通过做:
int counter = 0;
foreach (caseDB.usersRow usersRow in users)
{
if (usersRow.userId == selectedUser)
{
cbOwner.SelectedIndex = counter;
}
counter++;
}
答案 0 :(得分:5)
尝试分析
comboBox.SelectedValue = "value";
或
comboBox.SelectedItem = item;
问题编辑后:
将SelectedValue设置为selectedUser:
cbOwner.SelectedValue = selectedUser;
答案 1 :(得分:5)
使用DisplayMember
和ValueMember
属性时,请设置SelectedValue
。
否则,请设置SelectedItem
。