How can I remove a selected item from a listview?
-
24You might wanna accept some answers for your questions. – Mehrdad Afshari Sep 15 '09 at 07:19
-
1Possible duplicate of [UWP remove selected item from listview](https://stackoverflow.com/questions/49532128/uwp-remove-selected-item-from-listview) – Owen Pauling Mar 28 '18 at 13:03
7 Answers
foreach ( ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
where listView1 is the id of your listview.

- 184,426
- 49
- 232
- 263
When there is just one item (Multiselect = false
):
listview1.SelectedItems[0].Remove();
For more than one item (Multiselect = true
):
foreach (ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}

- 239,200
- 50
- 490
- 574
-
private void iAttachmentDel_Click(object sender, RoutedEventArgs e) { listBox.Items.RemoveAt(listBox.SelectedIndex); } – Marcin Aug 31 '16 at 12:57
Well, although it's a lot late, I crossed that problem recently, so someone might cross with this problem again. Actually, I needed to remove all the selected items, but none of the codes above worked for me. It always throws an error, as the collection changes during the foreach. My solution was like this:
while (listView1.SelectedIndex > 0)
{
listView1.Items.RemoveAt(listView1.SelectedIndex);
}
It won't throw the error as you get position of the last selected item (Currently), so even after you remove it, you'll get where it is now. When there is no items selected anymore, the SelectedIndex returns -1 and ends the loop. This way, you can make sure that there is no selected item anymore, nor that the code will try to remove an item in a negative index.

- 31
- 1
listView1.Items.Cast<ListViewItem>().Where(T => T.Selected)
.Select(T => T.Index).ToList().ForEach(T => listView1.Items.RemoveAt(T))

- 8,944
- 8
- 43
- 90
-
Thanks for your code, it helped me with some modification to my needs: var item = lstResult.Items.Cast
().Where(b => b.Content.ToString().Contains("Progresso")); if(item.Count()>0) lstResult.Items.Remove(item.ElementAt(0)); – Matheus Stumpf Mar 19 '19 at 18:13
Yet another way to remove item(s) from a ListView
control (that has GridView
) (in WPF
)--
var selected = myList.SelectedItems.Cast<Object>().ToArray();
foreach(var item in selected)
{
myList.Items.Remove(item);
}
where myList
is the name of your ListView
control

- 1,774
- 5
- 18
- 28
foreach (DataGridViewRow dgr in dgvComments.SelectedRows)
dgvComments.Rows.Remove(dgr);