c# - 如何从 Combobox 中设置选定的值?

标签 c# .net winforms data-binding combobox

我在 C# Windows 窗体中使用组合框。我绑定(bind)的项目列表如下:

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 中。现在我想从数据库项中设置所选值,例如:

cmbEmployeeStatus.SelectedValue =employee.employmentstatus;     

但是组合框没有选择值。我该怎么做?

最佳答案

试试这个。

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

希望对您有所帮助。 :)

关于c# - 如何从 Combobox 中设置选定的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688157/

相关文章:

c# - SqlGeography 空间操作缓慢 - SQL Server 2016

c# - 业务逻辑的正确位置在哪里?

c# - 我的 MS Access 更新查询有什么问题?

c# - 部分声明不能指定不同的基类?

c# - Azure 存储表 ExecuteAsync 在 Retrieve TableOperation 上挂起

winforms - Devexpress Xtragrid 如何将组合框编辑器添加到列

winforms - 在不存在控制对象的情况下在 UI 线程上运行代码

vb.net - WinForms TableLayoutPanel ComboBox 无法正确调整大小

c# - 在现有的 webforms 项目中使用 bundle

c# - 无限循环消耗 100% CPU