如何从组合框设置所选值?组合、所选

2023-09-02 11:58:09 作者:有你的幸福.

我在C#中的窗口形式使用下拉框。我绑定为下面的项目清单:

I use combobox in c# windows form. I bound the item list as below:

var employmentStatus = new BindingList<KeyValuePair<string, string>>();

employmentStatus.Add(new KeyValuePair<string, string>("0", "[Select Status]"));
employmentStatus.Add(new KeyValuePair<string, string>("1", "Contract"));
employmentStatus.Add(new KeyValuePair<string, string>("2", "Part Time"));
employmentStatus.Add(new KeyValuePair<string, string>("3", "Permanent"));
employmentStatus.Add(new KeyValuePair<string, string>("4", "Probation"));

employmentStatus.Add(new KeyValuePair<string, string>("5", "Other"));
cmbEmployeeStatus.DataSource = employmentStatus;
cmbEmployeeStatus.ValueMember = "Key";
cmbEmployeeStatus.DisplayMember = "Value";
cmbEmployeeStatus.SelectedIndex = 0;

我保存选择的值在数据库eg.1或2。现在我想从设置如数据库项目中选择值:

I save the selected value in database eg.1 or 2. Now I want to set selected value from database item like:

cmbEmployeeStatus.SelectedValue =employee.employmentstatus;     

不过,组合框不与价值选择。我该怎么办呢?

But combobox not selected with value. How can I do that?

推荐答案

试试这个。

cmbEmployeeStatus.SelectedIndex = cmbEmployeeStatus.FindString(employee.employmentstatus);

希望有所帮助。 :)

Hope that helps. :)