CancelEdit may throw exception if AllowRemove = False

CancelEdit may throw exception if AllowRemove = False

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


amselem posted on Tuesday, January 23, 2007

I've a BO derived from BusinessBase(Of T) wich has an editable child collection derived from BusinessListBase(Of T,C).

I've some code that toggles the AllowRemove value in this child collection, so I can add/remove childs and then set AllowRemove = False to block further editing
In that case calling the root BO .CancelEdit method throws a "Method not supported" exception. I've found that the CancelEdit method may call BindingList.RemoveAt (index) wich fails if AllowRemove = False.

To solve this problem I've changed the method BusinessListBase.UndoChanges from:

If child.EditLevelAdded > mEditLevel Then
   RemoveAt(index)
End If

to:

If child.EditLevelAdded > mEditLevel Then
   Dim CurrentAllowRemove As Boolean = Me.AllowRemove
   Try
      Me.AllowRemove = True
      RemoveAt(index)
   Finally
      Me.AllowRemove = CurrentAllowRemove
   End Try
End If

This way I force AllowRemove=True when calling CancelEdit/UndoChanges (but not outside it, I think that the exception thrown is ok if Remove/RemoveAt gets called directly).

I'd like to discuss this issue, what do you think about that code change ?

Regards,
Jacobo

RockfordLhotka replied on Thursday, January 25, 2007

This seems like a reasonable change to me, I'll make it in 2.1.2. Probably the last change I'll make before this version goes live.

Copyright (c) Marimer LLC