Click to See Complete Forum and Search --> : Listview ....very SLOW when adding items
vanki
Jan 8th, 2003, 03:05 PM
Hello,
Iīm adding maybe 2-500 items into a Listview and its very slow...
Anyone know a resolutiion to this problem....
My code is very plain just a For loop which adds items while the recordset is not EOF ... so i donīt really know what could be wrong ....
Thanks in advance
vanki
RealNickyDude
Jan 8th, 2003, 03:27 PM
Hi vanki :D
How about popping a copy of your code here for us to take a look at.
DSAINMON
Jan 8th, 2003, 04:53 PM
The problem might be that every time you add an item, the listview redraws itself, which can take a while. Before the loop, call the listview's beginUpdate() method and after the loop call its EndUpdate() method.
vanki
Jan 8th, 2003, 05:01 PM
For Each oResult In oSearch.SearchResults
System.Windows.Forms.Application.DoEvents()
lstvResults.BeginUpdate()
If bStop = True Then
Exit For
End If
Dim itmX As New ListViewItem()
Dim s As Integer
If oCols.Item(0).Name = "Size" Then
itmX.Text = CInt(oResult.Fields.Item(oCols.Item(0).Name).Value / 1024) & "KB"
Else
itmX.Text = CStr(oResult.Fields.Item(oCols.Item(0).Name).Value)
End If
itmX.Tag = oResult.DocumentID
If oResult.Extension = ".msg" Then
itmX.ImageIndex = 10
ElseIf oResult.Extension = ".doc" Then
itmX.ImageIndex = 8
ElseIf oResult.Extension = ".xls" Then
itmX.ImageIndex = 9
ElseIf oResult.Extension = ".ppt" Then
itmX.ImageIndex = 18
ElseIf oResult.Extension = ".mdb" Then
itmX.ImageIndex = 20
ElseIf oResult.Extension = ".gif" Or oResult.Extension = ".jpg" Or oResult.Extension = ".tif" Or oResult.Extension = ".bmp" Then
itmX.ImageIndex = 22
ElseIf oResult.Extension = ".pdf" Then
itmX.ImageIndex = 24
Else
itmX.ImageIndex = 13
End If
If oCols.Count() = 1 Or oCols.Count > 1 Then
For s = 1 To oCols.Count - 1
If oCols.Item(s).Name = "Size" Then
itmX.SubItems.Add(CInt(oResult.Fields.Item(oCols.Item(s).Name).Value / 1024) & "KB")
Else
itmX.SubItems.Add(CStr(oResult.Fields.Item(oCols.Item(s).Name).Value))
End If
Next
End If
lstvResults.Items.Add(itmX)
lstvResults.EndUpdate()
prgrBar.PerformStep()
counter = counter + 1
Next
Athley
Jan 9th, 2003, 01:16 AM
I agree with DSAINMON and I think the beginupdate and endupdate is good to check, but you have to keep it outside the loop, not inside as you are now. Now you are calling the methods for every round of the loop, saving nothing.
vanki
Jan 9th, 2003, 10:24 AM
I tried the BeginUpdate and EndUpdate and put it ouside of the for loop but it makes no difference ....
Edneeis
Jan 9th, 2003, 10:59 AM
You should also remove the DoEvents which will slow it down a lot.
lstvResults.BeginUpdate()
For Each oResult In oSearch.SearchResults
'System.Windows.Forms.Application.DoEvents()
If bStop = True Then
Exit For
End If
Dim itmX As New ListViewItem()
Dim s As Integer
If oCols.Item(0).Name = "Size" Then
itmX.Text = CInt(oResult.Fields.Item(oCols.Item(0).Name).Value / 1024) & "KB"
Else
itmX.Text = CStr(oResult.Fields.Item(oCols.Item(0).Name).Value)
End If
itmX.Tag = oResult.DocumentID
If oResult.Extension = ".msg" Then
itmX.ImageIndex = 10
ElseIf oResult.Extension = ".doc" Then
itmX.ImageIndex = 8
ElseIf oResult.Extension = ".xls" Then
itmX.ImageIndex = 9
ElseIf oResult.Extension = ".ppt" Then
itmX.ImageIndex = 18
ElseIf oResult.Extension = ".mdb" Then
itmX.ImageIndex = 20
ElseIf oResult.Extension = ".gif" Or oResult.Extension = ".jpg" Or oResult.Extension = ".tif" Or oResult.Extension = ".bmp" Then
itmX.ImageIndex = 22
ElseIf oResult.Extension = ".pdf" Then
itmX.ImageIndex = 24
Else
itmX.ImageIndex = 13
End If
If oCols.Count() = 1 Or oCols.Count > 1 Then
For s = 1 To oCols.Count - 1
If oCols.Item(s).Name = "Size" Then
itmX.SubItems.Add(CInt(oResult.Fields.Item(oCols.Item(s).Name).Value / 1024) & "KB")
Else
itmX.SubItems.Add(CStr(oResult.Fields.Item(oCols.Item(s).Name).Value))
End If
Next
End If
lstvResults.Items.Add(itmX)
prgrBar.PerformStep()
counter = counter + 1
Next
lstvResults.EndUpdate()
vanki
Jan 9th, 2003, 11:01 AM
I need the DoEvents so the user can stop the process
Edneeis
Jan 9th, 2003, 11:05 AM
I think you may be stuck with it being slow then, especially if its 500 items. Or maybe only do events on every third item or every 5 items or something. See DoEvents has to check and let all other system processes run so it is checking on EVERY item and slowing you down.
vanki
Jan 9th, 2003, 11:08 AM
I tried to comment out the DoEvents .... but the speed doesnt change at all
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.