VB.NET – Get the index of deleted projects from the binding list

I can get the index of the items added to the BindingList. When I try to get the index, if I delete the item I get an error

< pre>Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

This is my code

< pre>Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click

For i As Integer = 0 To _assignedSelection.SelectedCount-1
Dim item As Jurisdiction = CType(_assignedSelection.GetSelectedRow(i), Jurisdiction)
_list.Remove(item)
Next

End Sub


Private Sub list_Change(ByVal sender As Object, ByVal e As ListChangedEventArgs) Handles _list.ListChanged

If (_list.Count> 0) Then

Select Case e.ListChangedType
Case ListChangedType.ItemAdded
_dal.InsertJurisdiction(_list.Item(e.NewIndex))
Case ListChangedType.ItemDeleted
‘MsgBox(e.NewIndex.ToString)
_dal.DeleteJurisdiction(_list.Item(e.NewIndex)) <--------HERE
End Select

End If

End Sub

Edit: The answer in C# is also welcome… Anyone?

Before the event is triggered, the item will be deleted. This means (without additional code) you cannot Access the item to be deleted.

However, you can inherit from BindingList and override RemoveItem:

public class BindingListWithRemoving: BindingList 
{
protected override void RemoveItem(int index)
{
if (BeforeRemove != null)
BeforeRemove(this,
new ListChangedEventArgs (ListChangedType.ItemDeleted, index));

base.RemoveItem(index);
}

public event EventHandler BeforeRemove;
}

You should also copy the BindingList constructor. Also, don’t try to make it cancelable, because the caller may assume that calling delete does delete the item.

< p>I am able to get the index of the item added to the BindingList. When I try to get the index, if the item is deleted I get the error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

This is my code

Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd Remove.Click

For i As Integer = 0 To _assignedSelection.SelectedCount-1
Dim item As Jurisdiction = CType(_assignedSelection.GetSelectedRow(i), Jurisdiction)
_list.Remove (item)
Next

End Sub


Private Sub list_Change(ByVal sender As Object, ByVal e As ListChangedEventArgs) Handles _list.ListChanged

If (_list.Count> 0) Then


Select Case e.ListChangedType
Case ListChangedType.ItemAdded
_dal.InsertJurisdiction(_list. Item(e.NewIndex))
Case ListChangedType.ItemDeleted
'MsgBox(e.NewIndex.ToString)
_dal.DeleteJurisdiction(_list.Item(e.NewIndex)) <---- ----HERE
End Select

End If

End Sub

Edit: The answer in C# is also welcome…. Anyone ?

Before the event is triggered, the item will be deleted. This means (without additional code) you cannot access the item to be deleted.

However, you can inherit from BindingList and override RemoveItem:

public class BindingListWithRemoving: BindingList
{
protected override void RemoveItem(int index)
{
if (BeforeRemove != null)
BeforeRemove(this,
new ListChangedEventArgs(ListChangedType.ItemDeleted, index));

base.RemoveItem(index);
}

public event EventHandler BeforeRemove;
}

You should also copy the BindingList Constructor. Also, don’t try to make it cancelable, because the caller may assume that calling delete does delete the item.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3385 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.