Using a multi-select ListBox to delete records

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a ListBox bound to a DataSet. I'd like to be able to select two or
more items in the ListBox and delete the corresponding records in the Access
table which serves as the data source.

I have figured out how to edit multiple records, by using a Do...Loop
construct to check each item in turn, test its state and edit it if it's
selected. But I couldn't make that approach work when deleting records,
because of course the number of records, and the index of each, changes every
time a record is deleted. I know about the For Each...Next construct and the
SelectedIndices collection, but I can't figure out how to use them to achieve
the goal. Any ideas, anybody?
 
Użytkownik "Anonymouse" <[email protected]> napisaÅ‚ w
wiadomości (...)
I have figured out how to edit multiple records, by using a Do...Loop
construct to check each item in turn, test its state and edit it if it's
selected. But I couldn't make that approach work when deleting records,
because of course the number of records, and the index of each, changes
every
time a record is deleted. I know about the For Each...Next construct and
the
SelectedIndices collection, but I can't figure out how to use them to
achieve
the goal. Any ideas, anybody?

First possibility: instead of For Each use simple For loop, for example:
you have:
For Each dr As DataRow in dt.Rows
.....

change to:
Dim dr As DataRow, i As Integer
For i = 0 To dt.Rows.Count -1

Second possibility is to use isolated enumerator - Eric Gunnerson has
written about it in article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp01212002.asp

Regards,
Grzegorz
 
Hmm, that might work. Thanks!

Grzegorz Danowski said:
Użytkownik "Anonymouse" <[email protected]> napisaÅ‚ w
wiadomości (...)

First possibility: instead of For Each use simple For loop, for example:
you have:
For Each dr As DataRow in dt.Rows
.....

change to:
Dim dr As DataRow, i As Integer
For i = 0 To dt.Rows.Count -1

Second possibility is to use isolated enumerator - Eric Gunnerson has
written about it in article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp01212002.asp

Regards,
Grzegorz
 
Actually, my original code , which worked in the same way as the edit code,
was correct all along. Where I went wrong was trying to update the database
after *each* deletion, rather than after *all* of them. A For...Next loop
works in that case.
 
Back
Top