New Posts New Posts RSS Feed: Deleting an Entity In Custom BindingList
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Deleting an Entity In Custom BindingList

 Post Reply Post Reply
Author
Randar View Drop Down
Newbie
Newbie
Avatar

Joined: 13-May-2011
Location: Toronto
Posts: 23
Post Options Post Options   Quote Randar Quote  Post ReplyReply Direct Link To This Post Topic: Deleting an Entity In Custom BindingList
    Posted: 25-May-2011 at 12:53pm
I am trying to be clever and creating my own BindingList, which handles the adding/deleting of entity logic.  That way, when I bind it to simple UltraGrids (similar to DataGrids), they should delete themselves.  See http://www.ideablade.com/forum/forum_posts.asp?TID=2712&title=editable-grid-controls for some additional info.  The problem is, whenever I try to delete the entity via the aspect, I get an error I don't really understand.

Here is the key parts of the code:

''' <summary>
''' This class is a wrapper around BindingList, that is incorporated directly into the the EntityManager for new/delete operations.
''' </summary>
''' <typeparam name="T"></typeparam>
Public Class EntityBindingList(Of T)
    Inherits BindingList(Of T)   

    ''' <summary>
    ''' Removes the item at the specified index.
    ''' </summary>
    ''' <param name="index">The zero-based index of the item to remove.</param>
    ''' <exception cref="T:System.NotSupportedException">You are removing a newly added item and <see cref="P:System.ComponentModel.IBindingList.AllowRemove" /> is set to false. </exception>
    Protected Overrides Sub RemoveItem(ByVal index As Integer)
        OnRemovingItem(New RemoveItemEventArgs(Me(index)))

        ' Convert this to a entity so we can access the EntityAspect
        Dim entityToRemove As Entity = TryCast(Me(index), IdeaBlade.EntityModel.Entity)
        ' Flag the entity to be deleted when the EntityManager is saved
        entityToRemove.EntityAspect.Delete()

        MyBase.RemoveItem(index)
    End Sub

End Class


When stepping through the code, as soon as I try to call EntityAspect.Delete(), I get "Unable to delete the row: Cannot call BeginEdit() on Deleted entities."  Which makes no sense since I can see through the watch window, it is still flagged as "Unchanged".

I tried to not Delete it, thinking it might change it under the hood, but no luck. 

So what does the error mean?  Or even better, how do I fix it?
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 25-May-2011 at 4:26pm
Before deleting an entity, it's good practice to remove it from any bound lists. What happens if you move MyBase.RemoveItem(index) before entityToRemove.EntityAspect.Delete()?

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down