简体   繁体   中英

Adding Items to ListView too slow in C#

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

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.

Edit :

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 ?

You could make it much faster by enabling virtual mode .
However, that will take some work.

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

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.

Appologies for a more architectural solution, but if your domain objects are large this might cause the bottleneck (reading the comments it sounds like they may be slowing it down). Before you get to the presentation layer you could flatten them into some (very simple) domain transfer objects (DTOs): literally just a bag of getters-and-setters.

A tool like AutoMapper could potentially take a lot of the donkey work out there

That way your domain objects stay in the business logic domain (where they belong) and your presentation layer just gets the data in needs from the DTO.

Sorry for the non-code-based suggestion :) good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM