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.