N-Level Undo always leaves 1 row in the wrong state

N-Level Undo always leaves 1 row in the wrong state

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


GeorgeG posted on Monday, January 29, 2007

I have a  parent root BusinessListBase<SODCustomers, SODCustomer> where  BusinessBase<SODCustomer> . I am using windows DataGridView. In the search/fetch I have the following code

   sODCustomers=SODCustomers.GetSODCustomers(string parmeter)
   sODCustomers.BeginEdit()
   sODCustomersBindingSource.DataSource = sODCustomers;

In the cancel button I have this code
           sODCustomers.CancelEdit();
            sODCustomersBindingSource.CancelEdit();
            sODCustomers.BeginEdit();

The current edit level is 1 for all rows except for the selected cell which is 2.

If I change row 1 and row 2 and then click Cancel everything is ok. However, if change something in row1 and then row 2 and then change more data in row 1 and row 2, one row does not have the right data, it has the data from the second last changed. Have tried all combinations of bindingsource and object cancel edit and begin edit with no success.

What I am misssing?

 

RockfordLhotka replied on Monday, January 29, 2007

This is a known issue that has come up on the forums a few times before.

The key to success when using a BindingSource in Windows Forms data binding is to avoid directly calling the BeginEdit(), CancelEdit() and ApplyEdit() methods on your objects. Instead, always call the corresponding methods on the BindingSource object.

Also, when using any parent-child model, make sure you call the child's BindingSource before calling the parent to keep everything in proper order.

The one exception to all this, is if you have an editable root list. In that case you may want to call BeginEdit() manually before binding the object. Data binding will call BeginEdit() on each row as you edit it, and you need to do CancelEdit()/EndEdit() calls on the BindingSource to counter that. But having called BeginEdit() manually means you can manually call CancelEdit() to roll back all changes to the collection if needed.

GeorgeG replied on Tuesday, January 30, 2007

I did read the threads in this forum before posting but have not seen anything. I use editable root list. The problem is assume row1=Test1 and row2=Test2 and then update row1=Test1More and row2=Test2More and then go to row1=Test1MoreAddedMore and then hit cancel everything ids fine except that  row1=Test1More  instead of row1=Test1 .  Has anybody seen this behavior and what I am missing?

Thanks

DansDreams replied on Wednesday, January 31, 2007

GeorgeG:

I did read the threads in this forum before posting but have not seen anything. I use editable root list. The problem is assume row1=Test1 and row2=Test2 and then update row1=Test1More and row2=Test2More and then go to row1=Test1MoreAddedMore and then hit cancel everything ids fine except that  row1=Test1More  instead of row1=Test1 .  Has anybody seen this behavior and what I am missing?

Thanks

I'm presuming you're talking about editing the list in a grid.

This was precisely the behavior I experienced.  Did you find my thread among your search?  At the end of it I think you'll find I will have your solution.

Rocky has stated before that these issues end up being confusion about the way databinding works, and I'm a believer after many many hours of beating my head against the wall.  And I don't mean that to question your ability, it is not easy to understand.  :)

Yuriy Posidelov replied on Monday, March 26, 2007

Yeah, I have this issue too.

And I also beating my head against the wall :)

After all I make disicion:

If I have BusinessBase I use BeginEdit, CancelEdit of business object and it works fine.

In case when I have BusinessBaseList I will use BeginEdit() for BusinessObject.

But for canceling some like this:

supplierListBindingSource.RaiseListChangedEvents = false;

supplierListBindingSource.CancelEdit();

supplierListBindingSource.RaiseListChangedEvents = true;

supplierListBindingSource.ResetBindings(false);

And EndEdit(), CancelEdit() for Binding Source.

It was written before by Rocky.

Also before save changes we have to use EndEdit() for BindingSource.

And after saving we have to explicitly rebind datasource by:

supplierListBindingSource.DataSource = objShop.SupplierListChildren;

dgvSupplier.DataSource = supplierListBindingSource;

Also rebind data everywhere you add data (ResetBindings(false))

May be it all! (Probably I help someone don't waste more time)

 

GeorgeG replied on Monday, March 26, 2007

I think the problem was that I never called MarkAsChild() in the BusinessListBase although I never went back to verify it for sure.

I have switched to EditableRootListBase since it was more applicable.

Copyright (c) Marimer LLC