开发者

Winform BindingSources - Question

I have a windows form (net 2.0) with controls on it bound to an entity (w/ INotifyPropertyChanged) through a BindingSource..works.

On the same form I have a drop down list that is also wired up through a BindingSource..works

Here's a sample of the relevant code:

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

So when I change a 开发者_StackOverflowvalue in the drop down list I thought that the m_PlanItemLookupBindingSource Current property would change along with the PaymentPlanID property of the entity which does change.

Just a bit confused.

Thanks in advance, Stephen


The BindingSource takes the value in the control and sets it in the underlying source, which is the current object determined by the Position property of the BindingSource.

So when you select a value in the drop down list, the PaymentPlanID property of the underlying object is set with the new value selected. The underlying object is identified by the Current property in the BindingSource.

If you want to move the Current property to the object you select in the drop down list, you have to use the MoveFirst, MoveLast, MovePrevious or MoveNext methods or Position property on the BindingSource.

As I see it, you can do the following: in the event handler of the Changed or ValueChanged event on the drop down list, you are getting the index of the selected item, which index you can pass to the BindingSource.Position property.

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

You should remove the DataBinding that links the drop down list's sefrom the drop down list EditValue to the PaymentPlanID. This way the PaymentPlanId in the underlying object is not set to the selected value before the Position in the BindingSource is changed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜