CancelEdit Problem

CancelEdit Problem

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


yuhnkee posted on Thursday, May 10, 2007

I have read tons of posts about different situations on how cancel edit is not working and I am very confused if these are the same as my sissue.  I have a Collection of editable root objects.  I have a form with a grid displaying some of the info and textboxes displaying all of the details, both bound to a bindingsource.  I click on the grid(activating a new current item in the binding source) which is also reflected in the details section.  I edit the item in the details section and when I cancel or save (Child.Save() or Child.Cancel() everything works fine.  The problem is if I go to a diffent item and come back to dirty child.  When I save it works fine. When I call cancel it doesn't change the properties back to the original values(edit level is at 1 before the cancel is called).

If I edit something, change current item, come back edit more, Child.Cancel() only cancels the second change (yet edit level is at 1 before I call the cancel).

RockfordLhotka replied on Thursday, May 10, 2007

I think this post covers it pretty well.

Don't call BeginEdit at all.

Make sure to call CancelEdit on the child bindingsource before the parent bindingsource. I don't know what you mean by Child.Cancel(), but if you are calling CancelEdit on the child object itself you are doomed.

Make sure to call EndEdit on the child bindingsource before the parent bindingsource. I don't know what you mean by Child.Save(), but if you are calling ApplyEdit on the child object itself you are doomed.

You have to think of the bindingsources as being nested, because they are nested. So you have to unwind all calls from the inside (child) out (parent). And you must call the methods on the bindingsource objects, not on the business objects themselves. The UI code should (ideally) never interact with anything but the bindingsource controls.

Also be aware that there are bugs in the grid control, and there are hotfixes from Microsoft for some of them. I don't know the details, but others on the forum have found and mentioned these issues.

yuhnkee replied on Friday, May 11, 2007

Sorry, I think I confused myself and you. 

What I was trying to say was I have a collection of editable root objects.  I only have 1 bindingsource (no parent and child binding source).  I never call BeginEdit (on the collection, object in the collection or the bindingsoure) I thought it got called automatically.  Also I am using an Infragistics grid.   I am calling CancelEdit on an item in the collection.  I think you are telling me I shouldn't call CancelEdit on the business object but on the binding source.  I replaced collection[0].CancelEdit() with BindingSource.CancelEdit() and I still have the same behavior(works when I stay on the current item but If I come back and try to cancel it doesn't).  I am dealing with alot of records thus why I wanted to call cancel and apply on the object not the collection(or binding source).

Obviously I don't always use a binding source, I am not going to have a problem calling those functions on the business objects am I?

RockfordLhotka replied on Friday, May 11, 2007

When using an ERLB collection you shouldn’t have to manually call any *Edit() methods at all – they should be handled by data binding automatically. The only exception might be that you need to call bindingSource. EndEdit()) when the form is being closed to make sure the last row gets committed if that’s your desire. Otherwise that last row’s edit would be effectively canceled because it would never get saved.

 

Rocky

yuhnkee replied on Friday, May 11, 2007

I must be doing something else wrong because calling CancelEdit() on the binding source produces the same behavior..

yuhnkee replied on Friday, May 11, 2007

I figured it out, the EndEdit() was getting called when a different row got activated.

Copyright (c) Marimer LLC