【发布时间】:2011-10-05 00:20:09
【问题描述】:
我在 c# windows 窗体中使用组合框。我将项目列表绑定如下:
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;
但是组合框没有被选中。我该怎么做?
【问题讨论】:
-
我使用了你所有的答案,但组合框没有与文本一起出现。我在 form_Load 中绑定了组合框。我删除了文本出现在组合框中的绑定。为什么?
-
我认为您分配为 selectedItem 的值不正确。我在下面更新了我的答案。
标签: c# .net winforms data-binding combobox