Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview is too slow when loading

I am populating a list view with 1286 records, it takes about 34 seconds. i searched in internet and found this

 private void PopulateListViewWithCables(List<Cable> cables)
    {
        listView1.BeginUpdate();
        listView1.Items.Clear();
        System.Diagnostics.Stopwatch myStopWatch = new System.Diagnostics.Stopwatch();
        myStopWatch.Start(); 
        AddItemsToListView(cables);
        myStopWatch.Stop();
        string str;
        str = myStopWatch.Elapsed.Seconds.ToString();
        string s = str;
        listView1.EndUpdate();
    }


    private void AddItemsToListView(List<Cable> cables)
    {
        //Add to an ArrayList first because AddRange is a lot faster
        //than Add when dealing with lots of elements.
        //Also, there is no need to turn off the sorter when adding
        //all the elements at once

        ArrayList listItems = new ArrayList();

        foreach (Cable cable in cables)
        {
            // The if statement can be removed if all items in
            // MyDataClassCollection should be added to the ListView.               
            listItems.Add(CreateListViewItem(GetCableNavigation(cable)));
        }

        // Adds the items to the ListView
        listView1.Items.AddRange(
        (ListViewItem[])listItems.ToArray(typeof(ListViewItem)));
    }

    // Generate a listviewitem by using the myDataItem instance.
    private static ListViewItem CreateListViewItem(Cable cable)
    {
        ListViewItem item = new ListViewItem(
        new string[]
    {
    cable.Id.ToString(),
       cable.Item.ToString(),
       cable.GeneralFormat + cable.TagNo.ToString() + cable.EndString,
       cable.FromBay + cable.FromPanel, 
       cable.ToBay + cable.ToPanel,
       cable.CableProperty.Catalogue.Type,
       cable.CableProperty.Catalogue.CoreSize,
       cable.CableProperty.CableApplication.Application, 
       cable.CableRevision,
       cable.MinRequestCore.ToString(), 
       cable.Route, 
       cable.Distance.ToString(),
       ((CableStatusEnum)cable.Status).ToString(),
       cable.CableProperty.ProjectId.ToString(), 
       cable.CablePropertyId.ToString(),
       cable.TagNo.ToString(), 
       cable.GeneralFormat,
       cable.Length.ToString(), 
       cable.EndString, 
       cable.User.LastName, 
       cable.EditedOn.ToString()                
   });          
        return item;
    }

   private Cable GetCableNavigation(Cable cable)
    {
        CurrentInfo currentInfo = CurrentInfo.RecGetSingle();
        if (cable.CableProperty == null || cable.User == null)
        {
            using (CableServiceClient client = new CableServiceClient())
            {
                SearchElement[] criteria = new SearchElement[] { new SearchElement { Comparison = "=", FieldName = "Id", FieldValue = cable.Id, LogicalOperator = "" } };
                cable = client.GetCables(criteria, null, "CableProperty,CableProperty.Catalogue,CableProperty.CableApplication,User").SingleOrDefault();
                client.Close();
            }
        }
        return cable;
    }

i could reduce time of loading to 29 seconds, but it is still too much for 1286 records, how can i reduce the time of loading data?

Thank you

like image 702
nnmmss Avatar asked Dec 05 '25 13:12

nnmmss


1 Answers

Assuming this is a System.Windows.Forms.ListView, you should take a look at virtualization. Using virtualization will automatically do the lazy loading of the listview items for you, resulting in less memory usage, and better response time.

like image 114
Kris Vandermotten Avatar answered Dec 07 '25 15:12

Kris Vandermotten