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]
# 1

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

eebrown at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2
I tried adding

listView1.BeginUpdate();

before my loop but its still taking a long time when it gets to the

listView1.Items.Add(lstItem);

That's what I notice when I run it in degug mode.

I'll look at it some more. Thanks for the try.

MarcoB at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3

You might try creating your listview items and adding them all at once using ListView.Items.AddRange(). I believe this will cut down on the number of messages sent to the ListView, thus speeding up the process.

For instance,

ListViewItem[ ] lvItems = new ListViewItem[finalList.Length];

for (int index = 0; index < finalList.Length; index++)
{
ListViewItem lstItem = new ListViewItem(finalList[index].qNum.Trim());
lstItem.SubItems.Add(finalList[index].qDesc.Trim());
lstItem.SubItems.Add(finalList[index].qDesc2.Trim());
lstItem.SubItems.Add(finalList[index].qDesc3.Trim());

if ((_check || finalList[ index].alreadyChecked) && listView1.CheckBoxes)
lstItem.Checked = true;

lvItems[index] = lstItem;
}

listView1.BeginUpdate();
listView1.Items.AddRange(lvItems);
listView1.EndUpdate();

HTH,
Dan

dane_msft at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
AddRange method is not valid for the compact framework. nice try
MarcoB at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 5

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

eebrown at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 6

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

dane_msft at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 7

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

MarcoB at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 8

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

PeteVickers at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 9
good solution it do improve the performance by half although is still slow
hoilok at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...