gpt4 book ai didi

c# - 如何加速 Winforms ListView 项目删除?

转载 作者:行者123 更新时间:2023-11-30 12:12:49 25 4
gpt4 key购买 nike

我已经使用了 listView.BeginUpdate()listView.EndUpdate(),但是当我从 25k 中删除 100 个项目时,它仍然需要大约 10 秒。

有什么想法、技巧可以让它更快吗?

编辑:

this.listView.BeginUpdate();
for (int i = this.listView.CheckedItems.Count - 1; i > -1; --i)
{
this.listView.CheckedItems[i].Remove();
}
this.listView.EndUpdate();

最佳答案

您可以从这里开始优化:

List<int> toRemove = new List<int>();

foreach (ListViewItem item in this.listView.Items)
{
if (item.Checked) // check other remove conditions here
toRemove.Add(item.Index);
}

/* sort indices descending, so you'll remove items with higher indices first
and they will not be shifted when you remove items with lower indices */
toRemove.Sort((x, y) => y.CompareTo(x));
/* in this specific case you can simply use toRemove.Reverse();
or iterate thru toRemove in reverse order
because it is already sorted ascending.
But you might want to force sort it descending in some other cases.
*/

this.listView.BeginUpdate();

foreach (int itemIndex in toRemove)
this.listView.Items.RemoveAt(itemIndex); // use RemoveAt when possible. It's much faster with large collections

this.listView.EndUpdate();

关于c# - 如何加速 Winforms ListView 项目删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com