【发布时间】:2011-12-20 03:35:15
【问题描述】:
我想将项目添加到列表视图控件。这是一段代码:
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);
}
}
我这样做是因为我不想在使用多线程时挂起。不知道这对它有什么影响?
【问题讨论】:
-
哪一行是慢的部分? (使用调试器)
-
我尝试使用整数值,它以可接受的速度运行。 (