Problems with ListView Control

Peter King

Member
Joined
Sep 11, 2006
Messages
15
Programming Experience
Beginner
I am using listview control to display Excel-like table. The table is large,
with about 100000 rows, and each row has 10 columns. So I use ListView’s Details view option.

But I have 3 problems that have been puzzling me.

First, it is very slow to load data into the ListView Control.
I use the following codes to load a text file named ListViewFileName into Listview1.

VB.NET:
Dim fsStream As New FileStream(ListViewFileName, _
FileMode.Open, FileAccess.Read)
Dim srReader As New StreamReader(fsStream)
Try
Dim linestring As String
ListView1.BeginUpdate()
Dim j As Short
While srReader.Peek() > -1
linestring = srReader.ReadLine()
Dim linesplit() As String = Split(Trim(linestring), " ")
Dim Linelen As Integer = linesplit.Length - 1
If linesplit(0) <> "" Then
j = -1
Dim newline(Linelen) As String
For i As Short = 0 To Linelen
If linesplit(i) <> "" Then
j += 1
newline(j) = linesplit(i)
End If
Next
If j <> Linelen Then
ReDim Preserve newline(j)
End If
ListView1.Items.Add(New ListViewItem(newline))
End If
End While
ListView1.EndUpdate()
Catch ex As Exception
MyErrorMsgBox(ex.Message)
End Try
srReader.Close()
It takes about 5 minutes to load the data into the listview!

Second, it is even slower when I try to select some rows from listView1 and move them to ListView2. See the code below.

VB.NET:
Private Sub MoveItemsBetweenListViews(ByVal myListView1 As _
ListView, ByVal myListView2 As ListView)
If myListView1.Items.Count > 0 Then
Windows.Forms.Cursor.Current = Cursors.WaitCursor
Dim SelectedItems As ListView.SelectedListViewItemCollection = _
myListView1.SelectedItems
If SelectedItems.Count = 0 Then
MsgBox("No selection has been made!", _
MsgBoxStyle.Information, "Information")
Else
myListView1.BeginUpdate()
myListView2.BeginUpdate()
Dim ListItem As ListViewItem
For Each ListItem In SelectedItems
ListItem.Remove()
myListView2.Items.Add(ListItem)
Next
myListView1.EndUpdate()
myListView2.EndUpdate()
End If
Windows.Forms.Cursor.Current = Cursors.Default
End If
End Sub
Third, Listview control seems to take a lot of memory. For example, when the number of rows increases to 500000, my computer runs out of memory. However, the same file can be load perfectly into a
textbox, or RichtextBox, or MS-Word.

Can anyone there help with these problems?

Thanks a lot!

Peter
 
Last edited by a moderator:
Back
Top