views:

586

answers:

3

The ListChanged event for an IBindingList fires a type ListChangedType.ItemDeleted when items are deleted, perhaps by a user deleting a row in a datagrid control bound to the list. The problem is that the NewIndex into the list is invalid in this event, it's been deleted, and the item that was deleted is not available. There should be an ItemDeleting event, but I doubt they will ever fix this.

A: 

It's not really intended for that purpose. NewIndex is the index where the item was when it was deleted, and it's useful for bound controls to be able to locate their associated display item in their own lists and remove that.

What is the use case you want to enable with an ItemDeleting?

Sunlight
A: 

The control is already bound and updated since it initiated the delete. But IBindingList is not deleting the row from the underlying sql database, and this is where the design breaks down. I have to use one of the control events to delete the row. Seems like it would be a really nice design if I could use the IBindingList event to do this since it would be more general and encapsulated.

P a u l
A: 

Hi,

Yeah it's pretty annoying, but there is an easy workaround. I create a BindingListBase class that I use for all of my lists instead of using a normal BindingList. Because my class inherits from the BindingList, I have access to all of it's protected members, including OnRemovedItem. This enables me to pick up when an item is removed. You could do what I do and have a mRemovedItems list that I always add items to, or raise your own 'ItemRemoved' event.

See my code example below...


Public MustInherit Class BindingListBase(Of T)
    Inherits BindingList(Of T)

    Protected mRemovedItems As New List(Of T)

    Protected Overrides Sub ClearItems()
        MyBase.ClearItems()
        mRemovedItems.Clear()
    End Sub

    Protected Overrides Sub RemoveItem(ByVal index As Integer)
        Dim item As T = MyBase.Item(index)

        MyBase.RemoveItem(index)

        mRemovedItems.Add(item)
    End Sub

    Public ReadOnly Property RemovedItems as List(Of T)
        Get
            Return mRemovedItems
        End Get
    End Property
End Class


Adam Valpied