在C#中向ListView添加项目太慢

我想将项目添加到listview控件。 这是一些代码:

this.lView.ListViewItemSorter = null; ListViewItem[] lvitems = new ListViewItem[ListMyObjects.Count]; int index = 0; foreach (MyObject object in ListMyObjects) { ListViewItem item = new ListViewItem(); item.Text = object.Name; lvitems[index++] = item; } this.lView.BeginUpdate(); this.lView.Items.AddRange(lvitems); // Slow in here with debugger this.lView.EndUpdate(); 

我只添加了大约1000件物品,但速度非常慢。 它花了大约15秒完成。 为什么有人知道原因? 预先感谢。

编辑

我以前定制过listview。

 public partial class MyListView: ListView { public MyListView() { InitializeComponent(); this.View = View.Details; this.FullRowSelect = true; this.DoubleBuffered = true; } private bool mCreating; private bool mReadOnly; protected override void OnHandleCreated(EventArgs e) { mCreating = true; base.OnHandleCreated(e); mCreating = false; } public bool ReadOnly { get { return mReadOnly; } set { mReadOnly = value; } } protected override void OnItemCheck(ItemCheckEventArgs e) { if (!mCreating && mReadOnly) e.NewValue = e.CurrentValue; base.OnItemCheck(e); } } 

我这样做是因为当我使用multithreading时我不想挂起。 我不知道这会对它产生什么影响?

您可以通过启用虚拟模式来加快速度。
但是,这需要一些工作。

添加多个项目的首选方法是使用AddRange()方法 。 但是,如果必须逐个添加项目,则可以在循环中使用BeginUpdate()和EndUpdate()方法。 以下是来自MSDN

向ListView添加多个项目的首选方法是使用ListView.ListViewItemCollection的AddRange方法(通过ListView的Items属性访问)。 这使您可以在单个操作中将一组项添加到列表中。 但是,如果要使用ListView.ListViewItemCollection类的Add方法一次添加一个项目,则可以使用BeginUpdate方法阻止控件在每次添加项目时重新绘制ListView。

适用于更具体系结构的解决方案,但如果您的域对象很大,则可能会导致瓶颈(读取注释听起来可能会减慢它们的速度)。 在你到达表示层之前,你可以将它们展平成一些(非常简单的)域转移对象(DTO):字面上只是一袋吸气剂和制定者。

像AutoMapper这样的工具可能会占用很多驴工作

这样您的域对象就会保留在业务逻辑域(它们所属的位置)中,而您的表示层只是从DTO获取需要的数据。

对不起,基于非代码的建议:)祝你好运!