问题
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