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?
Printable View
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?
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.
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.
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?
Code?
Here is my code jm.
This is how i load the data: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
and this is how i show my 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
:)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
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.
I see..Thanks keystone paul and to jmcilhinney also. I will try to do that things. Have a nice day. :)
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.
ok jm..thanks again..i solved it at last...