如何通过键选择组合框中的项目?

时间:2017-08-15 17:57:41

标签: c# winforms c#-4.0 combobox

我使用以下方法填充组合框:

 //Setup data binding
            this.comboBox1.DataSource = dataSource;
            this.comboBox1.DisplayMember = "Name";
            this.comboBox1.ValueMember = "Value";

            var dataSource2 = new List<Status>();
            dataSource2.Add(new Status()
            {
                Name = "Первый раз",
                Value = "1"
            });
            dataSource2.Add(new Status()
            {
                Name = "Повторно",
                Value = "2"
            });

然后我尝试按键选择组合框中的项目:

comboBox1.SelectedItem = data.payment;

data.payment是字符串&#34; 2&#34 ;;

如何按键选择组合框中的项目?

2 个答案:

答案 0 :(得分:3)

comboBox.SelectIndex = comboBox.FindStringExact("Повторно")

comboBox.SelectedValue = "2"

答案 1 :(得分:1)

您可以使用 SelectedValue

comboBox.SelectedValue = "2"

或使用 Linq

   this.comboBox1.SelectedItem = dataSource2.SingleOrDefault(t=>t.Value == "2");
相关问题