471,599 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

Q: Removing selected items from a ListView?

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
7 34306
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
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
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
"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
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
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
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.

Similar topics

3 posts views Thread by Jeremy Owens-Boggs | last post: by
7 posts views Thread by GTi | last post: by
3 posts views Thread by ljlevend2 | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.