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?

Re: Using a multi-select ListBox to delete records by Grzegorz

Grzegorz
Fri Nov 12 04:12:02 CST 2004

Użytkownik "Anonymouse" <Anonymouse@discussions.microsoft.com> napisaÅ? w
wiadomoÅ?ci news:B136F039-C346-406C-9A21-9B62B2993FB4@microsoft.com...
(...)
> 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


Re: Using a multi-select ListBox to delete records by Anonymouse

Anonymouse
Fri Nov 12 11:36:31 CST 2004

Hmm, that might work. Thanks!

"Grzegorz Danowski" wrote:

> Użytkownik "Anonymouse" <Anonymouse@discussions.microsoft.com> napisaÅ? w
> wiadomoÅ?ci news:B136F039-C346-406C-9A21-9B62B2993FB4@microsoft.com...
> (...)
> > 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
>
>

Re: Using a multi-select ListBox to delete records by Anonymouse

Anonymouse
Fri Nov 12 12:39:01 CST 2004

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.