Results 1 to 11 of 11

Thread: [RESOLVED]Listview hang up while loading data on it

  1. #1

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Resolved [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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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?

  5. #5

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Re: Listview hang up while loading data on it

    Quote Originally Posted by jmcilhinney View Post
    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Listview hang up while loading data on it

    Code?

  7. #7

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Re: Listview hang up while loading data on it

    Quote Originally Posted by jmcilhinney View Post
    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

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  9. #9

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  11. #11

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    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
  •  



Click Here to Expand Forum to Full Width