Print Page | Close Window

Deleting an Entity In Custom BindingList

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2716
Printed Date: 30-Jul-2026 at 2:24am


Topic: Deleting an Entity In Custom BindingList
Posted By: Randar
Subject: Deleting an Entity In Custom BindingList
Date 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?



Replies:
Posted By: ting
Date 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()?




Print Page | Close Window