BeefTurkey
BeefTurkey

Reputation: 910

Remove selected rows from multi-column listView

I have a listview with two columns and I'm using a context menu to allow users to remove selected rows. To remove the selected rows, I've tried with the following code but it doesn't work:

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
    listView1.SelectedItems.Clear();
}

I suspect this is because the listview has two columns, but I can't figure out a solution to remove selected rows. Removing all rows works with: listView1.Items.Clear();.

Upvotes: 4

Views: 28484

Answers (9)

Rohan
Rohan

Reputation: 31

do
{ 
  this.listView1.CheckedItems[0].Remove(); 
} while (this.listView1.CheckedItems.Count > 0);

This works better

Upvotes: 0

GvS
GvS

Reputation: 52518

The latest example of BeefTurkey looks correct, but he should decrement the variable i after removing a selected item:

for (int i = 0; i < listView1.Items.Count; i++ )
{
    if (listView1.Items[i].Selected)
    {
        listView1.Items[i].Remove();
        i--;
    }
}

The index of items larger as i is decremented by 1 after the removal. So you should reposition i to match the next not tested item.

Upvotes: 6

Imamul Karim Tonmoy
Imamul Karim Tonmoy

Reputation: 584

 foreach(ListViewItem lvItem in lvDocument.SelectedItems)
 {
      lvDocument.Items.Remove(lvItem);
 }

Upvotes: 1

Max OfLondon
Max OfLondon

Reputation: 114

This is the correct way to remove all selected items. The method is to always access fist selected item with an index 0 and loop until no more selected items left. You cannot refer to other items inside collection with an absolute index safely since indexes will change as soon as you delete one of the items.

while( listView1.SelectedItems.Count > 0)
{
    listView1.Items.Remove(lvFiles.SelectedItems[0]);
}

Upvotes: 1

Jeff H
Jeff H

Reputation: 61

I have been using something slightly different then the others to remove all the selected items from a ListView control:

foreach (ListViewItem listViewItem in listView1.SelectedItems)
{
    listView1.Items.Remove(listViewItem);
}

I'm not sure how this would match up performance-wise to the other posted methods on large lists, but I think it is a little cleaner looking in cases where that isn't an issue.

Upvotes: 0

Ahmed
Ahmed

Reputation: 21

while (listBox1.SelectedItems.Count > 0)
{
    listBox1.Items.Remove(listBox1.SelectedItem);
}

Upvotes: 2

RvdK
RvdK

Reputation: 19790

What you can do:

foreach (ListViewItem Item in LstvClients.Items)
{    
     if (item.Selected)
     {
         LstvClients.Items.Remove(Item);
     }
}

(Yours is better, item.Remove())

Upvotes: 0

BeefTurkey
BeefTurkey

Reputation: 910

This seems to be a better solution:

for (int i = 0; i < listView1.Items.Count; i++ )
{
    if (listView1.Items[i].Selected)
    {
        listView1.Items[i].Remove();
    }
}

Upvotes: 0

BeefTurkey
BeefTurkey

Reputation: 910

This seems to work:

    for (int i = 0; i < listView1.Items.Count; i++ )
    {

        if (listView1.Items[i].Selected)
        {
            listView1.Items[i].SubItems.Clear();
        }

    }

Is there any way to remove items and re-order the listView so that there are no empty rows in the middle of other rows?

Upvotes: 0

Related Questions