我想将项目添加到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);
}
}
我这样做是因为当我使用多线程时我不想挂起。我不知道这会对它产生什么影响?
答案 0 :(得分:3)
您可以通过启用virtual mode来加快速度 但是,这需要一些工作。
答案 1 :(得分:2)
添加多个项目的首选方法是使用AddRange() method。但是,如果必须逐个添加项目,则可以在循环周围使用BeginUpdate()和EndUpdate()方法。以下是来自MSDN
向ListView添加多个项目的首选方法是使用ListView.ListViewItemCollection的AddRange方法(通过ListView的Items属性访问)。这使您可以在单个操作中将一组项添加到列表中。但是,如果要使用ListView.ListViewItemCollection类的Add方法一次添加一个项目,则可以使用BeginUpdate方法阻止控件在每次添加项目时重新绘制ListView。
答案 2 :(得分:0)
更具体系结构解决方案的应用,但如果您的域对象很大,这可能会导致瓶颈(读取评论听起来可能会减慢它的速度)。在进入表示层之前,您可以将它们展平为一些(非常简单的)域转移对象(DTO):实际上只是一大堆getter-and-setters。
像AutoMapper这样的工具可能会带来很多驴工作
这样您的域对象就会保留在业务逻辑域(它们所属的位置)中,而您的表示层只是从DTO获取需要的数据。
很抱歉非基于代码的建议:)祝你好运!