简体   繁体   English

在C#中从ListView检索数据时花费更多时间

[英]More Time taking While retriving data from the ListView in c#

Consider the ListView item Having two columns with more than 35,000 rows inserted already. 考虑ListView项,其中已经插入了两列,其中包含超过35,000行。 Now the user selects the any 30,000 rows from the ListView. 现在,用户从ListView中选择任何30,000行。 I want all the Selected Items data let's Say (Item,Subitems) 我想让所有“选定的项目”数据说(项目,子项目)

Here is my code. 这是我的代码。

ListView Name : lstViewData ListView名称 :lstViewData

        List<string> l_lstData = new List<String>();
        for(int l_nItem =0;l_nItem<lstViewData.SelectedItems.Count;l_nItem++)
        {
          l_lstData.Add(lstViewData.SelectedItems[l_nItem].Text+              
          lstViewData.SelectedItems[l_nItem].SubItems[1].Text);
        }

It Will take more than Half an hour in my PC(Configuration - IntelCore2Duo @2.40GHZ).Since the no. 在我的PC上将花费半小时以上的时间(配置-IntelCore2Duo @ 2.40GHZ)。 of selected items is morethan 30,000 in ListView ListView中选定项目的数量超过30,000

Kindly tell me the solution If any one knows? 请告诉我解决方案,如果有人知道吗?

You are using the + operator: 您正在使用+运算符:

l_lstData.Add(lstViewData.SelectedItems[l_nItem].Text+lstViewData.SelectedItems[l_nItem].SubItems[1].Text);

you should NEVER in a case where there is a lot of operations use the + operator on strings, as they are immutable. 如果有很多操作是不可变的,则不要在字符串上使用+运算符的情况下使用。 You should use a StringBuilder . 您应该使用StringBuilder

l_lstData.Add(sbuilder.Append(lstViewData.Seleteditems[l_nItem].Text).Append(lstViewData.SelectedItems[l_nItem].SubItems[1].Text).ToString());
sbuilder.Remove(0, sbuilder.Length);

where sbuilder is an instance of StringBuilder sbuilderStringBuilder的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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