c# - C# 中的 Listview 慢刷新

标签 c# winforms listview refresh

我有一个 ArrayList,它经常变化。

ListView 显示 ArrayList 的数据,因此当 ArrayList 更改时,必须快速更改此 ListView。

所以我写了这样的代码:

ArrayList ar;
ListView lv;

paintmethod()
{
  lv.items.clear();
  lv.addlistview(ar);
  lv.invalidate();
}

private void addlistview(ArrayList arr)
{
  lv.Items.Add(arr.somedata.tostring(),arr.somedata.tostring());
}

此代码有效,但当 ArrayList 更改时,ListView 不会立即刷新。

20秒、30秒甚至1分钟后刷新。

我该如何做更多的事情来解决这个问题?

最佳答案

尝试下面的内容,看看是否有更好的效果。使用 BeginUpdate() 是一个很好的做法和 EndUpdate()更新多个 listView 项目时。因为 BeginUpdate() 会阻止控件绘制,直到调用 EndUpdate 方法为止。

paintmethod()
{
    lv.BeginUpdate();

    lv.items.clear();
    lv.addlistview(ar);

    lv.EndUpdate();
}

The preferred way to add multiple items to a ListView is to use the AddRange method of the ListView.ListViewItemCollection (accessed through the Items property of the ListView). This enables you to add an array of items to the list in a single operation.

MSDN

这应该会大大提高性能。

关于c# - C# 中的 Listview 慢刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327943/

相关文章:

c# - 输入不是有效的 Base64 字符串,因为它包含非 base 64 字符

c# - c++/c# 命名管道中的问题

winforms - 如何以编程方式更改其集合中的 ListView 项键?

winforms - 如何避免 "&"下划线下一个字母?

android - 如何在 ExpandableList 中使用 ListView?

android - 从 ListArray 填充自定义 ListView

android - 如何在 Android 中实现分段列表?

c# - 如何在 linq 中执行 WHERE IN

c# - 如何修复 TreeView 中的 foreach

c# - 事件 ‘System.Windows.Controls.MediaElement.MediaOpened’只能出现在+ =或-=的左侧