是否可以根据其值设置组合框的selectedIndex

时间:2011-02-21 14:00:06

标签: c# .net winforms listbox selectedindex

是否可以根据其值设置组合框的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++;
            }

2 个答案:

答案 0 :(得分:5)

尝试分析

comboBox.SelectedValue = "value";

comboBox.SelectedItem = item;

问题编辑后:

将SelectedValue设置为selectedUser:

cbOwner.SelectedValue = selectedUser;

答案 1 :(得分:5)

使用DisplayMemberValueMember属性时,请设置SelectedValue

否则,请设置SelectedItem