adding items to listview is very slow
I have 300 items to add to the listview suing CF 1.0.
ListViewItem lstItem = new ListViewItem(finalList[ i ].qNum.Trim());
lstItem.SubItems.Add(finalList[ i ].qDesc.Trim());
lstItem.SubItems.Add(finalList[ i ].qDesc2.Trim());
lstItem.SubItems.Add(finalList[ i ].qDesc3.Trim());
if ((_check || finalList[ i ].alreadyChecked) && listView1.CheckBoxes)
lstItem.Checked = true;
listView1.Items.Add(lstItem);
I clear the listview and then we see the scroll bar getting smaller as the listview is filling up. Anyway to make it faster?
Thanks
[689 byte] By [
MarcoB] at [2007-12-23]
Yes, surround your "Add" loop with a BeginUpdate() and EndUpdate() call. This is part of the ListView class, and support by the Compact Framework.
Erik
Marco,
Your original question was how to keep the view from updating and scrollbar from growing smaller and smaller on your screen. The BeginUpdate / EndUpdate should do this. Can you confirm?
Your remaining problem appears to be that adding lots of list items, and doing subitems and calculations besides, takes a while. This is true.
I don't know what you're doing exactly, but can you do prep work ahead of time? For example, could you create your 300 list items in an array, or pre-Trim() all of your subitems, before you get to the loop. For example, create the array of 300 items, and then clear the list and add them all. Might take the same amount of time, but the user doesn't have to stare at a blank screen.
Hope this helps,
Erik
Aaargh. I hate it when that happens...
However, you can achieve the same effect by replacing AddRange with
foreach(ListViewItem lv in lvItems)
{
listView1.Add(lv);
}
The main idea was to do the prep work outside the control update as Erik has suggested.
Dan
sorry fot the delay but just came back from vacation...
the delay still takes a few minutes to load and that's only for 330 items.
there is a one second delay each time the code goes through this loop
foreach(ListViewItem lv in lvItems)
{
listView1.Items.Add(lv);
}
i'll keep looking. Thanks to all for the help
Hi,
just knocked up a quick sample in VB.Net, with an option to do beginupdate/endupdate as a checkbox. Can only run it on an emulator, as I have no PPC with me to test on.
To load 300 items without beginupdate/endupdate takes 6.2 seconds approx
To load 300 items WITH beginupdate/endupdate takes 2 seconds approx
The code is...
Dim ix As ListViewItem
Dim ict As Integer
ListView1.Items.Clear()
Dim st As Double = Environment.TickCount
If CheckBox1.Checked = True Then
ListView1.BeginUpdate()
End If
For ict = 0 To 300
ix = New ListViewItem
ix.Text = ict
ix.SubItems.Add("SI1 " & ict)
ix.SubItems.Add("SI2 " & ict)
ListView1.Items.Add(ix)
ix = Nothing
Next
If CheckBox1.Checked = True Then
ListView1.EndUpdate()
End If
MessageBox.Show(Environment.TickCount - st)
I remember doing quite a few tests in eVB on loading listboxes/listviews/grids(http://www.devbuzz.com/content/zinc_evb_performance_grid_pg1.asp), and found that the listview was generally quicker to load if you treated it with love and care
HTH
Pete