Slow ListView Performance due to painting

D

Danny T

In .net 2.0 (beta 1), I've got a ListView that contains 1000 items. I've
set it so when I click the heading of a column, it groups by that column.

All works fine, but because the listview is redrawing after every
change, it's quite slow. If I uncomment the .Visible lines, it's *very*
fast, but obviously the control is invisible for a moment (causes a
flicker).

Is there any way to stop the control redrawing/painting/invalidating
while I perform these loops other than hiding the control?

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
//listView1.Enabled = false;
foreach (ListViewItem l in listView1.Items)
{
bool newGroup = false;
newGroup = true;
foreach (ListViewGroup g in listView1.Groups)
{
if (g.Header == l.SubItems[e.Column].Text)
{
newGroup = false;
l.Group = g;
break;
}
}
if (newGroup)
{
ListViewGroup g = new
ListViewGroup(l.SubItems[e.Column].Text);
listView1.Groups.Add(g);
l.Group = g;
}
}
//listView1.Enabled = true;
}
 
N

Norman Yuan

ListView is known being slow to load items if more than a few hundreds. In
old VB5/6 time, you can set Visible to False to prevent the listview redraw
itself when each item is added. This would speed up loading quite a bit. In
..Net SuspendLayout()/ResumeLayout() is supposed to the trick, but I don't
know why it didn't. You can try to set Visisble false/true, simply change
two lines of your existing code listView.Enabled=false/true to
listView.Visisble=false/true.
 
D

Danny T

Norman said:
ListView is known being slow to load items if more than a few hundreds. In
old VB5/6 time, you can set Visible to False to prevent the listview redraw
itself when each item is added. This would speed up loading quite a bit. In
.Net SuspendLayout()/ResumeLayout() is supposed to the trick, but I don't
know why it didn't. You can try to set Visisble false/true, simply change
two lines of your existing code listView.Enabled=false/true to
listView.Visisble=false/true.

Thanks for the reply. I did actually use Visible - I changed it to
Enabled to see if that also worked, and forgot to change it back before
copying my code. Setting Visible to false makes a horrible flash -
though I guess it's better than being slow. I could always do it
conditionally based on how many items there are!
 
B

Bert

copying my code. Setting Visible to false makes a horrible flash - though
I guess it's better than being slow. I could always do it conditionally
based on how many items there are!

Maybe you can prevent that by using SuspendLayout/ResumeLayout on the
Listview's parent, like:

Me.SuspendLayout()
Me.Listview1.visible=false
....build listview
Me.listview1.visible=true
me.resumelayout()


Bert
 
D

Danny T

Bert said:
Maybe you can prevent that by using SuspendLayout/ResumeLayout on the
Listview's parent, like:

Me.SuspendLayout()
Me.Listview1.visible=false
...build listview
Me.listview1.visible=true
me.resumelayout()

Possibly - I'll give it a go later - ta! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top