C# 从多列 listView 中删除选定的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/546045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove selected rows from multi-column listView
提问by BeefTurkey
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();
.
我怀疑这是因为列表视图有两列,但我想不出删除选定行的解决方案。删除所有行适用于:listView1.Items.Clear();
。
采纳答案by GvS
The latest example of BeefTurkey looks correct, but he should decrement the variable i
after removing a selected item:
BeefTurkey 的最新示例看起来是正确的,但他应该i
在删除所选项目后递减变量:
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.
i
删除后,较大项目的索引减 1。所以你应该重新定位i
以匹配下一个未测试的项目。
回答by BeefTurkey
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?
有没有办法删除项目并重新排序 listView 以便其他行中间没有空行?
回答by BeefTurkey
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();
}
}
回答by RvdK
What you can do:
你可以做什么:
foreach (ListViewItem Item in LstvClients.Items)
{
if (item.Selected)
{
LstvClients.Items.Remove(Item);
}
}
(Yours is better, item.Remove())
(你的更好,item.Remove())
回答by Ahmed
while (listBox1.SelectedItems.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
回答by Jeff H
I have been using something slightly different then the others to remove all the selected items from a ListView control:
我一直在使用一些与其他稍微不同的东西来从 ListView 控件中删除所有选定的项目:
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.
我不确定这将如何在性能方面与大型列表中的其他已发布方法相匹配,但我认为在这不是问题的情况下,它看起来会更清晰一些。
回答by Max OfLondon
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.
这是删除所有选定项目的正确方法。该方法是始终访问索引为 0 的第一个选定项并循环,直到没有更多选定项为止。您不能安全地使用绝对索引引用集合中的其他项目,因为一旦您删除其中一个项目,索引就会更改。
while( listView1.SelectedItems.Count > 0)
{
listView1.Items.Remove(lvFiles.SelectedItems[0]);
}
回答by Imamul Karim Tonmoy
foreach(ListViewItem lvItem in lvDocument.SelectedItems)
{
lvDocument.Items.Remove(lvItem);
}
回答by Rohan
do
{
this.listView1.CheckedItems[0].Remove();
} while (this.listView1.CheckedItems.Count > 0);
This works better
这效果更好