2013-04-20 52 views
2

我想我差點弄明白了,但我有一個錯誤:通過DataSource屬性的綁定使綁定數據源中的第一個元素成爲組合的默認值,但這對我不利,因爲此默認值將被傳播到通過「SelectedValue」屬性綁定的數據源的第一行,並用錯誤的值覆蓋正確的值。如何解決這個問題?如何綁定從數據源獲取元素的組合框的「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"; 

回答

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

相關問題