2010-01-15 35 views
1

我有一個窗體窗體(net 2.0),通過一個BindingSource.works綁定到一個實體(w/INotifyPropertyChanged)上。Winform BindingSources - 問題

在相同的形式我有一個下拉列表也通過BindingSource..works

這裏連接好是有關的代碼示例:

m_PlanItemLookupBindingSource.DataSource = GetBusinessLogic().RetrievePaymentPlanLookups(); // Collection of PaymentPlans 
paymentPlanType.Properties.DataSource = m_PlanItemLookupBindingSource; 
paymentPlanType.Properties.DisplayMember = "Name"; 
paymentPlanType.Properties.ValueMember = "ID"; 
paymentPlanType.DataBindings.Add(new Binding("EditValue", m_PlanBindingSource, "PaymentPlanID", true, DataSourceUpdateMode.OnPropertyChanged, null, "D")); 

agencyComission.DataBindings.Add(new Binding("EditValue", m_PlanBindingSource, "AgencyCommission", true, DataSourceUpdateMode.OnPropertyChanged, null, "P1")); 
billingType.DataBindings.Add(new Binding("Text", m_PlanBindingSource, "BillingType")); 

所以,當我改變的值在下拉列表中,我認爲m_PlanItemLookupBindingSource Current屬性將隨着實際更改的實體的PaymentPlanID屬性一起更改。

只是有點困惑。

由於提前, 斯蒂芬

回答

1

BindingSource的取入控制值,並將其設置底層源,它是由所述的BindingSource的位置屬性所確定的當前對象英寸

因此,當您在下拉列表中選擇一個值時,基礎對象的PaymentPlanID屬性將使用所選的新值進行設置。底層對象由BindingSource中的Current屬性標識。

如果要將Current屬性移動到您在下拉列表中選擇的對象,則必須在BindingSource上使用MoveFirst,MoveLast,MovePrevious或MoveNext方法或Position屬性。

正如我所看到的那樣,您可以執行以下操作:在下拉列表中的Changed或ValueChanged事件的事件處理程序中,您將獲得所選項目的索引,您可以將該索引傳遞給BindingSource。位置屬性。

Changed or ValueChanged event handler 
    ... 
    int index = DropDownList.ListIndex 
    BindingSource.Position = index 
    ... 
End event handler 

您應該刪除該降的下拉列表中的EditValue到PaymentPlanID鏈接下拉列表中的sefrom數據綁定。這樣,在BindingSource中的Position被改變之前,底層對象中的PaymentPlanId沒有被設置爲選定的值。