[RESOLVED] ListView Speed Problem
I am adding ListViewItems to a listView from a SQLDataReader. The problem is that when the number of items grows, the time to add them to the listbox grows exponentially.
For example, when I add 22 items, the SQL Query takes 40ms and the loop to insert data takes 1300ms
For 125 items, it's 60ms For the SQL Query and 99 895ms for the loop
For 250 items, it's 1030ms for the SQL Query and 470 000ms for the loop
I suspend the layout of the listview during insertion and there is no calculation in the loop, only a few conditions.
What am I missing ? I can't see anything wrong in my code which goes roughly like this :
VB Code:
Redim strItem(nbColumn-1)
myListView.SuspendLayout
RS=myConnect.executeReader(Query)
Do While RS.Read
strItem(0) = rs("id")
strItem(1) = rs("client")
...
item = New ListViewItem(strItem)
Select Case strItem(x)
Case SomeCondition
item.BackColor = Color.Black
item.ForeColor = Color.White
Case OtherCondition
item.BackColor = Color.Red
item.ForeColor = Color.Black
Case Else
item.BackColor = Color.White
item.ForeColor = Color.Black
End Select
myListView.Items.Add(item)
Loop
myListView.ResumeLayout
Any Help would be great. Thanks
Re: ListView Speed Problem
I think you're supposed to use ListView.BeginUpdate and ListView.EndUpdate
Re: ListView Speed Problem
It doesn't change anything at all. I assume that the effect of ListView.BeginUpdate is the same as ListView.SuspendLayout. It just doesn't allow the control to redraw unless ListView.EndUpdate Or Listview.ResumeLayout is called
Re: ListView Speed Problem
Quote:
Originally Posted by matt3011
What am I missing ? I can't see anything wrong in my code which goes roughly like this :
VB Code:
Redim strItem(nbColumn-1)
myListView.SuspendLayout
RS=myConnect.executeReader(Query)
Do While RS.Read
strItem(0) = rs("id")
strItem(1) = rs("client")
...
item = New ListViewItem(strItem)
Select Case strItem(x)
Case SomeCondition
item.BackColor = Color.Black
item.ForeColor = Color.White
Case OtherCondition
item.BackColor = Color.Red
item.ForeColor = Color.Black
Case Else
item.BackColor = Color.White
item.ForeColor = Color.Black
End Select
myListView.Items.Add(item)
Loop
myListView.ResumeLayout
Any Help would be great. Thanks
1) Maybe you can add items first, then change color after u insert all items.
2) Add item, then add subitem. do not just use 'item = New ListViewItem(strItem)', stritem is an array, it may slow down ur program.
I used listview in my program, it should not be so slow.
Re: ListView Speed Problem
Ok, I pointed the problem out. I have a ListView.ListViewItemSorter set on my control, and it is active when inserting data.
I now set it to nothing when inserting data and I set it back when finished.
I have to wait 6s to sort my listview now. Does Anyone know what algorithm MS uses in their Sort() Method, I think it's in n², so maybe it should be interesting if someone had implemented one in n.log(n).