Cancel() on BindingSourceNode doesn't seem to work properly.

Cancel() on BindingSourceNode doesn't seem to work properly.

Old forum URL: forums.lhotka.net/forums/t/10594.aspx


njwessing posted on Friday, August 05, 2011

I have been struggling with edit level problems the past few days since I can only find scarce information on the subject.  I have a BO with an child list and the objects in that child list each have their own child list. I'm using BindingSourceHelper and BindingSourceNode to be able to edit the details of a SizeConfig object in a popup window. This window allows me to add template objects as well as element objects to each template object. Using BindingSourceNode.Apply() works great on my objects after I have made edits to them. However Cancel() seems to only undo the last change I have made in that window. Even if I make no changes the edit levels are higher than they should be.

Here is a sample of what I am doing in code:

void Form_Load() {

            bsDropDown = BindingSourceHelper.InitializeBindingSourceTree(components, bsList);

            bsDropDown.Bind(NameValueList);

            bsRoot = BindingSourceHelper.InitializeBindingSourceTree(components, bsSizeConfig);

            bsRoot.Bind(CurrentSize);

}

void okButton_Click(){

bsRoot.Apply();

bsRoot.Close();

bsList.Apply();

bsList.Close();

}

void cancelButton_Click(){

bsRoot.Cancel(CurrentSize);

bsDropDown.Cancel(NameValueList)

}

Here is what the data model looks like:

SizeConfig - Object that I am editing

Template - Child list of SizeConfig

Element - Child list of Template

 

I have a ComboBox that is bound to bsList that is used to change an id on SizeConfig.

I have a DataGridView that is used to show the Templates belonging to the SizeConfig. Bound to bsTemplates.

I have a second DataGridView that is used to show the Elements belonging to the selected Template. Bound to bsElements.

 

bsSizeConfig - bound to the edited object

bsTemplates - bound to bsSizeConfig - Templates

bsElements - bound to bsTemplates - Elements

 

When I Cancel() on the object the edit levels are as follows:

SizeConfig - 3

Template - 2

Element - 2

 

When I Apply() on the object the edit levels all correctly reset to 0. 

I really have no idea why the BindingSourceNode is not working for me. Hopefully someone reading this has had a similar issue and knows what to do to solve this. Thanks in advance for any help.

JonnyBee replied on Monday, August 08, 2011

Hi,

Your code should never call BeginEdit/EndEdit/CancelEdit on objects that are active in DataBinding to the UI.

DataBinding/BindingSource assumes it is the "one and only" owner of the data objects and will actually call BeginEdit/EndEdit/CancelEdit  very often.

You should only call these methods before databound or after disconnected from UI.

njwessing replied on Monday, August 08, 2011

In the view that calls this window I am suspending the binding sources before I open this window. I'm not actually calling begin/end/cancel edit on my objects. I'm using the BindingSourceNode class to help me. For some reason Apply() works perfectly but cancel does not.

Copyright (c) Marimer LLC