How do you programmatically add items to a list being edited?

How do you programmatically add items to a list being edited?

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


mike1j1 posted on Monday, July 06, 2009

Suppose you have a business base root object, which is a parent.  The parent is being edited and some of its properties are bound to a grid.  One of the properties is a business base list child.

As a result of receiving a message another process, a new object is created that is to be added to the child collection.  What is the approved technique for adding the object?

Making the obvious call of:
theParentObject.TheChildCollectionProperty.Add(theNewChildObject) blows up, throwing an UndoException due to an edit level mismatch.

RockfordLhotka replied on Monday, July 06, 2009

  1. This is in a smart client scenario (WPF or WinForms)?
  2. And you are using CSLA 3.6.x?
  3. And you are using managed backing fields for your properties (or at least for the child property)?

If that is all true, then it should work automatically - barring nasty data binding issues in Windows Forms (which is possible).

If 1-3 is not all true, the simplest thing may be to make 1-3 all be true :)

If 1-3 are all true, and you are using Windows Forms, you could be running into a Windows Forms data binding issue. They can be hard to work through, because you can easily end up fighting with data binding in unexpected and varied ways.

mike1j1 replied on Thursday, July 09, 2009

1-3 are all true.  I'm having trouble making any progress here - is there a good technique for debugging this?

My root object is bound via a BindingSourceNode, but is not directly bound to any widget.  Several of its children are bound to datagridviews, etc.  using the child relationship where they are bound to a BindingSourceNode that is in turn bound to the BindingSourceNode of the root.

The error is definitely related to binding - if I call Add on the child before I bind the root, the Add is successful.

I see this error for several different properties.  In one case, the property is a business list child of the root, and the child not bound to anything.  However, when I programmatically add to the child (after previously have bound to the root), I still see the edit level mismatch.

Copyright (c) Marimer LLC