如何从下拉框中设置所选值?

48

我在C# Windows窗体中使用了ComboBox控件。我按如下方式绑定了选项列表:

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;

我将选择的值保存在数据库中,例如1或2。现在我想从数据库项设置所选的值:

cmbEmployeeStatus.SelectedValue =employee.employmentstatus;     

但是下拉框没有选中值。我该如何做?


1
我使用了你的所有答案,但是组合框没有出现文本。我在form_Load中绑定了组合框。当我删除绑定时,文本出现在组合框中。为什么? - thinzar
我认为您分配为selectedItem的值不正确。我已在下面更新了我的答案。 - CharithJ
11个回答

0

在ComboBox中设置值

cmbEmployeeStatus.Text="Something";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接