0

I have a list of items. I parse through this list and based on a condition, parse elements from this list and save them in 2 separate lists - listOne and listTwo (of type - ListViewItem). Later, I call AddRange on both of these lists to add them in separate listViews - listViewOne and listViewTwo. Here is the code -

listViewOne.Sorting = System.Windows.Forms.SortOrder.Ascending;
listViewTwo.Sorting = System.Windows.Forms.SortOrder.Ascending;

private void AddRangeExample()
        {
           // MessageBox.Show("say what!!");
            ListViewItem NewlyCreatedItem = null;

            var listOne = new List<ListViewItem>();
            var listTwo = new List<ListViewItem>();

            listViewTwo.BeginUpdate();
            listViewTwo.Items.Clear(); 

            listViewOne.BeginUpdate();
            listViewOne.Items.Clear();

            for (int i = 1; i <= count; i++)
            {
                // code to fetch item from main list. 

                if (//condition in main list)
                {
                    if (// condition)
                    { 
                        NewlyCreatedItem = new ListViewItem(// element from main list);
                        listOne.Add(NewlyCreatedItem);
                    }
                    else
                        NewlyCreatedItem = new ListViewItem(//element from main list);
                        listTwo.Add(NewlyCreatedItem);
                    }
                }
            }

            ListViewItem[] listOneArr = listOne.ToArray();
            ListViewItem[] listTwoArr = listTwo.ToArray();

            listViewOne.Items.AddRange(listOneArr);
            listViewOne.EndUpdate();

            listViewTwo.Items.AddRange(listTwoArr);
            listViewTwo.EndUpdate();
        }

Now, if instead of using the AddRange, I keep adding the items to the listviews - listViewOne and listViewTwo within the for loop only, then everytime it adds the values to the listview, it will sort the entire list. If there are several thousand elements, then it would take too much time. Hence, AddRange is suggested which would stop drawing and sorting of elements unless all EndUpdate() is called and sort the entire listview just once. However, AddRange is taking the same time in this case.

Can someone please take a look and tell what could be going wrong as using AddRange should have speeden up things.

user2769790
  • 123
  • 1
  • 17
  • 1
    It may help if you call BeginUpdate() on the dialog these are in too. And call BeginUpdate() on everything, then do your code, then EndUpdate() on everything. – David Thielen Jan 29 '18 at 22:58
  • I tried putting BeginUpdate and EndUpdate on the dialogs these are in, but to no vain. – user2769790 Jan 31 '18 at 22:01
  • What if you turn sorting off, call all your adds, then turn sorting on. Although that may not help either because you're not changing the work to sort, just when it happens. – David Thielen Feb 01 '18 at 16:17
  • Doesn't AddRange automatically turn sorting off, adds all the items, and then turn default sorting " on" later? – user2769790 Feb 01 '18 at 17:32
  • That was my point - that you can turn sorting off, add one by one, then turn it back on. However, in that case you may not have saved time, just moved when that happens. Because the work has to occur to sort the list. – David Thielen Feb 01 '18 at 19:17

0 Answers0