我想将项目添加到列表视图控件。这是一段代码:在ListView中添加项目太慢C#
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秒完成。 为什么有人知道原因?预先感谢。
编辑:
我以前定制的列表视图。
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);
}
}
我这样做是因为我不想在我使用多线程时挂起。我不知道这对它有什么影响?
哪一行是较慢的部分? (使用调试器) – SLaks 2011-12-20 03:39:16
我认为是这样的...... – SLaks 2011-12-20 03:42:09
我用整数值试过了,它以可接受的速度运行。 (<2秒)。也许它与你的对象有关? – Matthias 2011-12-20 03:42:34