如何从组合框设置所选值? [英] How to set selected value from Combobox?

查看:163
本文介绍了如何从组合框设置所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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. :)

这篇关于如何从组合框设置所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆