Bindingsource is not suspending binding

荒凉一梦 提交于 2019-12-17 17:15:53

问题


I have a form that displays custom details, one section being a list of bank accounts associated with the customer. This list is bound to it's own bindingsource, so when loading a customer I do:

bsCustomer.DataSource = customer;
bsCustomerAccounts.DataSource = customer.Accounts;

I have an ObjectListView that is bound to bsCustomerAccounts. So far everything works fine.

To edit a particular account, I double-click on it and open a separate form:

using (var form = new CustomerAccountForm(selectedAccount))
{
    DialogResult result = form.ShowDialog(this);
    if (result == DialogResult.OK)
    {
        selectedAccount= form.Account;
    }
}

The problem is when the user clicks on Cancel to cancel the editing of the account in this form. The original bcCustomerAccounts and therefore the list are still being updated.

I've tried SuspendBinding and RaiseListChangedEvents = false but the bindingsource is still being updated.

Am I missing something?


回答1:


It seems really surprising at first, you think while you didn't assign the edited object back to the list, why is the list item edited?

The key point is here: Classes are Reference Type.

You passed selected account to the edit form and since it's a class and classes are reference type, in fact you are editing the same instance which is in the list. So when you edit properties, all edits are directly applying to the object regardless of clicking OK or Cancel.



来源:https://stackoverflow.com/questions/38187441/bindingsource-is-not-suspending-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!