项目添加到ListView控件在C#太慢 [英] Adding Items to ListView too slow in C#

查看:254
本文介绍了项目添加到ListView控件在C#太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加项目ListView控件。这是一个有点code的:

I want to add item to listview control. This is a bit of code :

    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项目,但它的速度非常缓慢。它花费约15secs来完成。
为什么没有人知道原因吗?预先感谢。

I'm only add about 1000 item but it's very slowly. It's spend about 15secs to finish. why does anyone know the reason ? Thank in advance.

修改

我以前定制列表视图。

I have customized listview before.

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);
    }   
}

我做,因为我不希望当我使用多个线程挂起。我不知道是什么影响呢?

I do it because i don't want to hang when i use multiple threading. I don't know what does this influenced to it ?

推荐答案

添加多个项目的preferred的方法是使用的的AddRange()方法。但是,如果你必须逐个增加的项目之一,你可以使用的的BeginUpdate()并在你的循环EndUpdate()方法。以下是从MSDN

The preferred way of adding multiple items is to use the AddRange() method. However if you must add the items one by one you can use the BeginUpdate() and EndUpdate() methods around your loop. Following is from the MSDN

将多个项目添加到ListView的preferred的方法是使用ListView.ListViewItemCollection(通过ListView控件的项目属性来访问)的方法的AddRange。这使您可以在一个操作中的项目的数组添加到列表中。但是,如果你想使用ListView.ListViewItemCollection类的Add方法添加的项目之一的时间,你可以从每一个项目被添加时重绘ListView中使用的BeginUpdate方法prevent控制。

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. However, if you want to add items one at a time using the Add method of the ListView.ListViewItemCollection class, you can use the BeginUpdate method to prevent the control from repainting the ListView every time that an item is added.

这篇关于项目添加到ListView控件在C#太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆