-
Apr 27th, 2009, 06:44 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED]Listview hang up while loading data on it
this is my problem, i have a listview and everytime I load a 10,000 data my listview hang up. Is there any alternative on how to load data to listview that will not hang up. Could anyone help me on this?
Last edited by shyguyjeff; Apr 28th, 2009 at 11:36 PM.
-
Apr 27th, 2009, 06:50 AM
#2
Re: Listview hang up while loading data on it
My initial reaction would be to not load 10,000 rows into it.
No one is going to want to wade through all that to find what they want anyway. I'm sure there is some kind of filtering you could employ to cut down the number of records loaded.
-
Apr 27th, 2009, 06:59 AM
#3
Re: Listview hang up while loading data on it
An alternative to what? If we don't know how you're doing it now how can we provide an alternative.
That said, if you're adding the items one by one then you need to call BeginUpdate and EndUpdate one either side. Better still, don't add them one by one. Add all the ListViewItems to an array and then call AddRange. Of course, the documentation for the Add method says all this so you would already have seen it when you read the documentation.
Last edited by jmcilhinney; Apr 27th, 2009 at 07:02 AM.
-
Apr 27th, 2009, 07:03 AM
#4
Re: Listview hang up while loading data on it
Do you really need to load 10,000 items into a listview?
How practical is that going to be as a user interface?
Have you thought about loading pages of say 100 records and then allowing the user to step forwards and backwards through pages?
-
Apr 27th, 2009, 07:21 AM
#5
Thread Starter
Hyperactive Member
Re: Listview hang up while loading data on it
Originally Posted by jmcilhinney
An alternative to what? If we don't know how you're doing it now how can we provide an alternative.
That said, if you're adding the items one by one then you need to call BeginUpdate and EndUpdate one either side. Better still, don't add them one by one. Add all the ListViewItems to an array and then call AddRange. Of course, the documentation for the Add method says all this so you would already have seen it when you read the documentation.
I load my listview using array and coming from the textfile jm.
@keystone_paul
you're right also paul maybe it's another option but as for now i want to load all the data to my listview.
-
Apr 27th, 2009, 07:32 AM
#6
Re: Listview hang up while loading data on it
-
Apr 27th, 2009, 08:04 AM
#7
Thread Starter
Hyperactive Member
Re: Listview hang up while loading data on it
Originally Posted by jmcilhinney
Code?
Here is my code jm.
Code:
Private Sub OpenTxtfile()
Dim errorInfo As String = [String].Empty
'open text file into Dataset:
Dim textFilePath As String = mFileName
Dim dataTextFile As New DataSet("textfile")
SetupListView(dataTextFile)
If Not LoadTextFile(textFilePath, dataTextFile, errorInfo) Then
MessageBox.Show("Failed to load text file:" & vbLf & errorInfo, "Load Text File")
Return
Else
MessageBox.Show(("File Loaded:" & vbLf & "Tables:" & dataTextFile.Tables.Count.ToString() & vbLf & "Rows:") + dataTextFile.Tables(0).Rows.Count.ToString(), "Load Text File")
End If
'setup list view from columns from Dataset:
SetupListView(dataTextFile)
'show data in list view:
If Not ShowDataInListView(dataTextFile, errorInfo) Then
MessageBox.Show("Failed to display text file (listview) :" & vbLf & errorInfo, "Load Text File")
Return
End If
dataTextFile.Dispose()
ListView1.Sorting = SortOrder.Ascending
End Sub
This is how i load the data:
Code:
Private Function LoadTextFile(ByVal textFilePath As String, ByVal dataToLoad As DataSet, ByRef errorInfo As String) As Boolean
errorInfo = [String].Empty
Try
Dim textFileFolder As String = (New System.IO.FileInfo(textFilePath)).DirectoryName
Dim textConnectionString As String = ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=") + textFileFolder & ";" & "Extended Properties=""text;"";"
'from using System.Data.OleDb:
Dim textConnection As New OleDbConnection(textConnectionString)
textConnection.Open()
textFilePath = (New System.IO.FileInfo(textFilePath)).Name
Dim selectCommand As String = "select * from " & textFilePath
'open command:
Dim textOpenCommand As New OleDbCommand(selectCommand)
textOpenCommand.Connection = textConnection
Dim textDataAdapter As New OleDbDataAdapter(textOpenCommand)
Dim rows As Integer = textDataAdapter.Fill(dataToLoad)
textConnection.Close()
textConnection.Dispose()
Return True
Catch ex_load_text_file As Exception
errorInfo = ex_load_text_file.Message
Return False
End Try
End Function
and this is how i show my data.
Code:
Private Function ShowDataInListView(ByVal dataToShow As DataSet, ByRef errorInfo As String) As Boolean
errorInfo = [String].Empty
Try
'ListView1.Items.Clear()
For Each textRow As DataRow In dataToShow.Tables(0).Rows
Dim li As New ListViewItem(textRow(0).ToString())
For i As Integer = 1 To dataToShow.Tables(0).Columns.Count - 1
li.SubItems.Add(textRow(i).ToString())
Next
ListView1.Items.Add(li)
Next
Return True
Catch ex_show_data As Exception
errorInfo = ex_show_data.Message
Return False
End Try
End Function
-
Apr 27th, 2009, 08:10 AM
#8
Re: Listview hang up while loading data on it
It looks to me that you aren't adding to the listview in a single shot as JMcilhinney suggested - you are adding them one at a time, which will take forever.
Instead of adding them directly to the listview create an array of ListViewItems and add your records from the text file into that.
Then once you have all of the records add them all to the listview control by using ListView1.Items.AddRange(myarray)
But the bottom line is that if you are reading 10,000 entries from a textbox and then adding them to a listview it isn't going to happen in the blink of an eye.
The way your current code works will take ages - just create a simple for...next loop of 1 to 100000 and to a listview.items.add "test" within that loop and see how long it takes. I tried this and after 30 seconds hit CTL_BREAK to see how far through it was and it was only halfway through the loop, and thats without doing any data retrieval or adding subitems. Then I switched to adding my entries to an array of ListViewItems and the whole process took less than 2 seconds.
That approach will speed up your routine massively but it will still take time, and I would seriously question the value of loading up a list of 10,000 items in one go.
NB there are lots of other things you could do to speed up your code, for example in your ShowDetailsInListView routine you are checking "dataToShow.Tables(0).Columns.Count - 1" once for every row, ie 10,000 times. Presumably this doesn't change at all during the loop so if you were to create a variable outside the loop and set it to dataToShow.Tables(0).Columns.Count - 1 and then use your variable in the For...Next loop you will save an awful lot of processor cycles.
Last edited by keystone_paul; Apr 27th, 2009 at 08:18 AM.
-
Apr 27th, 2009, 08:28 AM
#9
Thread Starter
Hyperactive Member
Re: Listview hang up while loading data on it
I see..Thanks keystone paul and to jmcilhinney also. I will try to do that things. Have a nice day.
-
Apr 27th, 2009, 06:50 PM
#10
Re: Listview hang up while loading data on it
I suggest that you take a look at my BackgroundWorker thread in the VB.NET CodeBank forum. Follow the advice in my signature to find it. One of the examples it provides is creating a list of items in a background thread and then adding them to a ListBox in a batch at the end. You can modify that to add items to a ListView.
-
Apr 28th, 2009, 07:14 AM
#11
Thread Starter
Hyperactive Member
Re: Listview hang up while loading data on it
ok jm..thanks again..i solved it at last...
Last edited by shyguyjeff; Apr 28th, 2009 at 11:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|