gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:59:48 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com