Remove the Selected Item From ListView

前端 未结 7 1646
野的像风
野的像风 2021-01-18 06:29

How can I remove a selected item from a listview?

相关标签:
7条回答
  • 2021-01-18 07:01

    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);
    }
    
    0 讨论(0)
  • 2021-01-18 07:02
    listView1.Items.Cast<ListViewItem>().Where(T => T.Selected)
        .Select(T => T.Index).ToList().ForEach(T => listView1.Items.RemoveAt(T))
    
    0 讨论(0)
  • 2021-01-18 07:06
    foreach (DataGridViewRow dgr in dgvComments.SelectedRows)
                dgvComments.Rows.Remove(dgr);
    
    0 讨论(0)
  • 2021-01-18 07:09
    foreach ( ListViewItem eachItem in listView1.SelectedItems)
    {
        listView1.Items.Remove(eachItem);
    }
    

    where listView1 is the id of your listview.

    0 讨论(0)
  • 2021-01-18 07:11
    listBox.Items.RemoveAt(listBox.SelectedIndex);
    
    0 讨论(0)
  • 2021-01-18 07:12

    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

    0 讨论(0)
提交回复
热议问题