How can I remove a selected item from a listview?
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);
}
listView1.Items.Cast<ListViewItem>().Where(T => T.Selected)
.Select(T => T.Index).ToList().ForEach(T => listView1.Items.RemoveAt(T))
foreach (DataGridViewRow dgr in dgvComments.SelectedRows)
dgvComments.Rows.Remove(dgr);
foreach ( ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
where listView1 is the id of your listview.
listBox.Items.RemoveAt(listBox.SelectedIndex);
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