【问题标题】:ComboBox.SelectedValue not workingComboBox.SelectedValue 不起作用
【发布时间】:2013-09-14 19:59:04
【问题描述】:

这段代码有什么问题?

myComboBox.Items.Clear();
myComboBox.Items.AddRange(new string[]{"one","two"});
myComboBox.SelectedValue = "one";

它显示时没有选择任何内容。

【问题讨论】:

  • Asp.net, win forms, wpf?

标签: c# winforms combobox


【解决方案1】:

如果您像这样填充组合框:

myComboBox.Items.AddRange(new string[]{"one","two"});

您必须使用ComboBox.SelectedItemComboBox.SelectedIndex 属性来设置/获取所选项目:

myComboBox.SelectedItem = "one"; //or
myComboBox.SelectedIndex = 0; 

ComboBox.SelectedValue 属性继承自 ListControl 并且必须在以下情况下使用:

  • 控件绑定到DataSource
  • ValueMemberDisplayMember 属性已定义。

【讨论】:

    【解决方案2】:

    几个不同的选项:

    1) 将SelectedValue 更改为SelectedIndex

    myComboBox.SelectedIndex = 0; //your first item
    

    请忽略这个,这是针对asp.net的

    2) 手动添加ListItems

    myComboBox.Items.Clear();
    myComboBox.Items.Add(new ListItem() { Text = "one", Selected = true };
    myComboBox.Items.Add(new ListItem() { Text = "two" };
    

    只需确保您在给定时间选择的项目不超过一项。

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2019-07-22
      • 2012-08-23
      • 1970-01-01
      • 2012-03-09
      • 2010-09-19
      相关资源
      最近更新 更多