1

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

2 回答 2

7

试试看

comboBox.SelectedValue = "value";

或者

comboBox.SelectedItem = item;

问题编辑后:

将 SelectedValue 设置为 selectedUser:

cbOwner.SelectedValue = selectedUser;
于 2011-02-21T14:04:02.600 回答
5

使用DisplayMemberandValueMember属性时,设置SelectedValue.

否则,设置SelectedItem.

于 2011-02-21T14:06:15.823 回答