By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Q: Removing selected items from a ListView?

P: n/a
Hi!

I'v been struggeling with removing selected items from a listview.

Anyone that can give me a piece of code that does this?

I am a newbee to this C# and cant figure it out....

Regards
Martin Arvidsson
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
This should do the trick. It just loops through the ListView controls
SelectedItems collection and passes that item to its Remove method.

foreach(System.Windows.Forms.ListViewItem eachItem in
this.listView1.SelectedItems)
{
this.listView1.Items.Remove(eachItem);
}

Nov 17 '05 #2

P: n/a
Hi,

Well, The below code does work for a DataTable, I bet it will work for a
listview as you will have the exact same issues.

The problem is that you cannot do this:
foreach(ListViewItem item in listview.SelectedItem)
listview.Items.Delete( item );
cause you are modifying the collection and iterating at the same time ( an
exception will be throw IIRC ).

Solution:
Use a temp list
ArrayList elems = new ArrayList()
foreach(ListViewItem item in listview.SelectedItem)
elems.Add( item );

foreach(ListViewItem item in elems)
listview.Items.Delete( item );


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.net> wrote in
message news:OQ**************@TK2MSFTNGP10.phx.gbl...
Hi!

I'v been struggeling with removing selected items from a listview.

Anyone that can give me a piece of code that does this?

I am a newbee to this C# and cant figure it out....

Regards
Martin Arvidsson

Nov 17 '05 #3

P: n/a
Hi,

IMO this will not work, you are modifying a collection at the same time you
are iterating it. if you delete an item then that item is not longer
selected so the collection change and the iteration fails
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"gmiley" <gm****@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
This should do the trick. It just loops through the ListView controls
SelectedItems collection and passes that item to its Remove method.

foreach(System.Windows.Forms.ListViewItem eachItem in
this.listView1.SelectedItems)
{
this.listView1.Items.Remove(eachItem);
}

Nov 17 '05 #4

P: n/a
"Ignacio Machin ( .NET/ C# MVP )" wrote...
ArrayList elems = new ArrayList()
foreach(ListViewItem item in listview.SelectedItem)
elems.Add( item );
It seems overkill to iterate through the items to copy the references to
another list. Shouldn't this work just as well?

ArrayList elems =
new ArrayList( listview.SelectedItems );

foreach(ListViewItem item in elems)
listview.Items.Delete( item );

// Bjorn A
Nov 17 '05 #5

P: n/a
It works fine, SelectedItems is a seperate collection. It does not
remove the item from the SelectedItems collection, it removes it from
the Items collection.

Nov 17 '05 #6

P: n/a
Hi ,

It should be the same, only that it's the constructor of ArrayList where
the iteration occurs

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
"Ignacio Machin ( .NET/ C# MVP )" wrote...
ArrayList elems = new ArrayList()
foreach(ListViewItem item in listview.SelectedItem)
elems.Add( item );


It seems overkill to iterate through the items to copy the references to
another list. Shouldn't this work just as well?

ArrayList elems =
new ArrayList( listview.SelectedItems );

foreach(ListViewItem item in elems)
listview.Items.Delete( item );

// Bjorn A

Nov 17 '05 #7

P: n/a
Hi,

Yes, but I bet that the selectedItem collection is modified depending of
the Items collection, there should be a method in ListView that hook in the
Add/Delete, etc events of Items, when It does change it will most probably
recreate the Selected collection iterating in the Items collection and
checking for the Selected property.

Again, I have not tested this, a simple test shoudl say if the above is
correct or not, It does happens with a DataTable / DataView , beside the
above algorithm makes perfectly sense.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"gmiley" <gm****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
It works fine, SelectedItems is a seperate collection. It does not
remove the item from the SelectedItems collection, it removes it from
the Items collection.

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.