c# - 加载时Listview太慢

标签 c# winforms listview

我正在用 1286 条记录填充 ListView ,大约需要 34 秒。我在互联网上搜索并找到了这个

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

我可以将加载时间减少到 29 秒,但是对于 1286 条记录来说还是太多了,我怎样才能减少加载数据的时间?

谢谢

最佳答案

假设这是一个 System.Windows.Forms.ListView,您应该看看 virtualization .使用虚拟化会自动为您延迟加载 ListView 项,从而减少内存使用量并缩短响应时间。

关于c# - 加载时Listview太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19658911/

相关文章:

c# - 更改winform应用程序中所有表单的背景颜色

c# - C# 中的多态性。解释输出

c# - 从 .NET 应用程序使用 OTRS TicketConnector

c# - 将 UTC 日期时间转换为另一个时区

c# - WPF Listview 交替行背景按数据组而不是行

java - Android:如何创建N个 ListView ?

listview - 如何删除特定索引中 ListView 中的小部件?

c# - Response.Write(RSS) 导致 Javascript 异常 : "Unspecified error" (IIS 7. 5)

c# - Windows 窗体并行绘图

c# - .NET - 如何获取所有视频格式而不是指定每种格式