如何绑定从数据源获取其元素的组合框的“SelectedValue”属性?

时间:2013-04-20 19:05:36

标签: c# winforms data-binding combobox

我想我差点搞清楚,但我有一个错误:通过DataSource属性的绑定使得绑定数据源中的第一个元素成为组合的默认值,但这对我不利,因为这个默认值将是传播到通过“SelectedValue”属性绑定的数据源的第一行,并使用错误的值覆盖正确的值。怎么解决这个问题?

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

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";

1 个答案:

答案 0 :(得分:0)

我这样做:

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();         
    }
  }

在recordChange中执行此操作

相关问题