My user is able to select multiple items in a ListView
collection that is configured to show details (that is, a list of rows).
我的用户能够在ListView集合中选择多个项目,这些项目配置为显示详细信息(即行列表)。
What I want to do is add a Delete
button that will delete all of the selected items from the ListViewItem
collection associated with the ListView
.
我想要做的是添加一个删除按钮,该按钮将删除与ListView关联的ListViewItem集合中的所有选定项目。
The collection of selected items is available in ListView.SelectedItems
, but ListView.Items
doesn't appear to have a single method that lets me delete the entire range. I have to iterate through the range and delete them one by one, which will potentially modify a collection I'm iterating over.
ListView.SelectedItems中提供了所选项的集合,但ListView.Items似乎没有一个方法可以删除整个范围。我必须遍历范围并逐个删除它们,这可能会修改我正在迭代的集合。
So, what I'm basically after is the opposite of AddRange()
.
所以,我基本上追求的是AddRange()的反面。
4 个解决方案
#1
If you have a collecion of your selectedItems, you can simply call remove on each of them instead of iterating over the ListView.
如果你有selectedItems的集合,你可以简单地在每个集合上调用remove而不是迭代ListView。
There is a method on ListViewItem called Remove().
ListViewItem上有一个名为Remove()的方法。
ListView listview = <reference to ListView>;
foreach (ListViewItem item in listView.SelectedItems)
{
item.Remove();
}
This removes the iteration through all the items and only removes your selected items.
这将删除所有项目的迭代,并仅删除所选项目。
#2
As far as I know, there is no way other than to delete them individually.
据我所知,除了单独删除它们之外别无他法。
Wrap your deletion algorithm in ListView.BeginUpdate()
and ListView.EndUpdate()
calls. This way you won't get the slowdown of repainting on each delete.
将删除算法包装在ListView.BeginUpdate()和ListView.EndUpdate()调用中。这样您就不会在每次删除时重新获得重新绘制的速度。
Additionally, if you delete them in reverse order, I believe the underlying structure will have less to do.
另外,如果你以相反的顺序删除它们,我相信底层结构将没什么用。
Also, if the number of items you are deleting is a significant percentage of the total number of items, you might get more performance by clearing the list and AddRange()
ing them back again.
此外,如果您要删除的项目数占项目总数的很大一部分,则可以通过清除列表并再次使用AddRange()将其获得更高的性能。
#3
For me, in linq 2 sql I use:
对我来说,在linq 2 sql中我使用:
mylist.Clear();
#4
Perhaps you could try something along the lines of the following:
也许您可以尝试以下方面的内容:
var ItemsToDelete = ... // wherever you get the collection of items to delete from
var RemainingItems = yourList.FindAll(delegate(ListItem x) {
return !ItemsToDelete.Contains(x)
});
yourListView.DataSource = RemainingItems;
yourListView.DataBind();
or just assign the remaining items to the existing control, or whatever you prefer.
或者只是将剩余的项目分配给现有的控件,或者您喜欢的任何内容。
(edited formatting slightly)
(稍微编辑格式)
#1
If you have a collecion of your selectedItems, you can simply call remove on each of them instead of iterating over the ListView.
如果你有selectedItems的集合,你可以简单地在每个集合上调用remove而不是迭代ListView。
There is a method on ListViewItem called Remove().
ListViewItem上有一个名为Remove()的方法。
ListView listview = <reference to ListView>;
foreach (ListViewItem item in listView.SelectedItems)
{
item.Remove();
}
This removes the iteration through all the items and only removes your selected items.
这将删除所有项目的迭代,并仅删除所选项目。
#2
As far as I know, there is no way other than to delete them individually.
据我所知,除了单独删除它们之外别无他法。
Wrap your deletion algorithm in ListView.BeginUpdate()
and ListView.EndUpdate()
calls. This way you won't get the slowdown of repainting on each delete.
将删除算法包装在ListView.BeginUpdate()和ListView.EndUpdate()调用中。这样您就不会在每次删除时重新获得重新绘制的速度。
Additionally, if you delete them in reverse order, I believe the underlying structure will have less to do.
另外,如果你以相反的顺序删除它们,我相信底层结构将没什么用。
Also, if the number of items you are deleting is a significant percentage of the total number of items, you might get more performance by clearing the list and AddRange()
ing them back again.
此外,如果您要删除的项目数占项目总数的很大一部分,则可以通过清除列表并再次使用AddRange()将其获得更高的性能。
#3
For me, in linq 2 sql I use:
对我来说,在linq 2 sql中我使用:
mylist.Clear();
#4
Perhaps you could try something along the lines of the following:
也许您可以尝试以下方面的内容:
var ItemsToDelete = ... // wherever you get the collection of items to delete from
var RemainingItems = yourList.FindAll(delegate(ListItem x) {
return !ItemsToDelete.Contains(x)
});
yourListView.DataSource = RemainingItems;
yourListView.DataBind();
or just assign the remaining items to the existing control, or whatever you prefer.
或者只是将剩余的项目分配给现有的控件,或者您喜欢的任何内容。
(edited formatting slightly)
(稍微编辑格式)