【问题标题】:How to bind the "SelectedValue" property of a combobox that gets his elements from a datasource?如何绑定从数据源获取其元素的组合框的“SelectedValue”属性?
【发布时间】:2013-04-13 23:53:41
【问题描述】:

我想我几乎想通了,但我有一个错误:通过 DataSource 属性的绑定使绑定数据源中的第一个元素成为组合的默认值,但这对我不利,因为此默认值将是传播到通过“SelectedValue”属性绑定的数据源的第一行,并用错误的值覆盖正确的值。如何解决?

这是我的代码:(在北风数据库中,我希望能够从组合中选择将插入订单中的员工)

this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.ordersBindingSource, "EmployeeID", true));
this.comboBox1.DataSource = this.employeesBindingSource;
this.comboBox1.DisplayMember = "FullName";
this.comboBox1.ValueMember = "EmployeeID";

【问题讨论】:

    标签: c# winforms data-binding combobox


    【解决方案1】:

    我这样做:

    DataRow dr=this.ordersBindingSource.Current;
      Combo.Text="";
      if(dr!=null)
      {
        if(dr[Combo.ValueMember]!=DBNull.Value)
        {
         Combo.SelectedValue=dr[Combo.ValueMember].ToString();        
         Combo.Text=dr[Combo.DisplayMember].ToString();         
        }
      }
    

    在记录更改中执行此操作

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 2016-11-01
      • 2013-09-20
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      相关资源
      最近更新 更多