Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Speeding up TreeView.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Resolved [RESOLVED] [2005] Speeding up TreeView.

    I have a treeView in my application that displays the available ebay categories, as you can imagine this is quite sizeable, around 2500 dataTable rows.

    The problem I have is when a parent category is expanded there is a delay (not massive but enough to be annoying) while the application goes through the dataTable and pulls out the relevent sub categories and any associated child items.

    I've not done a great deal with treeviews so the method I'm using to populate it may not be the best.

    If anyone could point me in the right direction for eliminating or at least minimising the delay .....

    The code for the treeview is as follows:

    vb Code:
    1. Public Class Categories
    2.  
    3.     Dim dt As DataTable
    4.     Dim dr As DataRow
    5.     Dim tn As TreeNode
    6.  
    7.     Private Sub Categories_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         dt = Main.dtCats
    9.         'if the category list is still being downloaded show the please wait message.
    10.         lblMessage.Visible = True
    11.         trvCats.ImageList = ImageList1
    12.     End Sub
    13.  
    14.     Private Sub Categories_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    15.         If dt.Rows.Count > 1 Then
    16.             loadLIst()
    17.         End If
    18.     End Sub
    19.  
    20.     '///
    21.     'This method loads the initial top level parents and any associated child items
    22.     '///
    23.     Public Sub loadList()
    24.         'Hide the please wait message
    25.         lblMessage.Visible = False
    26.         For Each dr In dt.Rows
    27.             If dr("level") = "1" Then
    28.                 tn = New TreeNode
    29.                 tn.Text = dr("name")
    30.                 tn.Name = dr("CatID")
    31.                 'check if the node is a leaf category, if so set the images required
    32.                 If dr("leaf") Then
    33.                     tn.ImageIndex = 2
    34.                     tn.SelectedImageIndex = 2
    35.                 End If
    36.                 trvCats.Nodes.Add(tn)
    37.                 AddChildNodes(tn)
    38.             End If
    39.         Next
    40.     End Sub
    41.  
    42.     '///
    43.     'This method add the child nodes to the parent node
    44.     '///
    45.     Private Sub AddChildNodes(ByVal tn As TreeNode)
    46.         If tn.Nodes.Count = 0 Then
    47.             Dim childNode As TreeNode
    48.             For Each dr As DataRow In dt.Rows
    49.                 'find the rows that have the same parent ID but not the same name
    50.                 'as this will add a duplicate of the parent category to the list
    51.                 If dr("ParentID") = tn.Name AndAlso dr("name") <> tn.Text Then
    52.                     childNode = New TreeNode
    53.                     childNode.Text = dr("name")
    54.                     childNode.Name = dr("CatID")
    55.                     'if the category is a leaf then add the appropriate image
    56.                     If dr("leaf") Then
    57.                         childNode.ImageIndex = 2
    58.                         childNode.SelectedImageIndex = 2
    59.                     End If
    60.                     'add the node to the collection
    61.                     tn.Nodes.Add(childNode)
    62.                 End If
    63.             Next
    64.         End If
    65.     End Sub
    66.  
    67.     Private Sub trvCats_AfterCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvCats.AfterCollapse
    68.         'node has been collapsed so reset the image displayed
    69.         e.Node.ImageIndex = 0
    70.     End Sub
    71.  
    72.     Private Sub trvCats_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvCats.AfterSelect
    73.         'get the id of the selected node
    74.         tn = e.Node
    75.         AddChildNodes(tn)
    76.         For Each node As TreeNode In tn.Nodes
    77.             AddChildNodes(node)
    78.         Next
    79.     End Sub
    80.  
    81.     Private Sub trvCats_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles trvCats.BeforeExpand
    82.         'get the id of the selected node
    83.         tn = e.Node
    84.         'node is being expanded so change the image displayed
    85.         tn.ImageIndex = 1
    86.         AddChildNodes(tn)
    87.         For Each node As TreeNode In tn.Nodes
    88.             AddChildNodes(node)
    89.         Next
    90.     End Sub
    91. End Class
    If my post helps , please feel free to rate it

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Speeding up TreeView.

    I have seen a few good examples in some previous posts. You might achieve better results by using recursion. It doesn't appear that you are currently using recursion in your code.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: [2005] Speeding up TreeView.

    I've haven't been able to use .NET for a few months, and I don't have it in front of me now, but are there some sort of BeginUpdate() and EndUpdate() methods on the TreeView? Also, it might speed it up if before you enter any loops you find out what index the various columns are in the data row, and then use that index instead of always looking them up by key.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [2005] Speeding up TreeView.

    Thanks for the input, I'll give the suggestions a try and post my findings.
    If my post helps , please feel free to rate it

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [2005] Speeding up TreeView.

    Perfect ....recursion was the answer, my treeview now responds instantly, many thanks
    If my post helps , please feel free to rate it

  6. #6
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: [RESOLVED] [2005] Speeding up TreeView.

    Did you just load everything all at once? I don't see how recursion has sped anything up, other than possibly shifting the load time from when the user is interacting with it to the initialization period?
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [RESOLVED] [2005] Speeding up TreeView.

    No, what I did was instead of going through every datarow in the datatable searching for matching sub categories I first of all limited the number of datarows using 'datatable.select(filterString)' Where 'filterString is the parent category ID, this has made he whole thing work alot faster.

    I used this thread as a guide:

    http://www.vbforums.com/showthread.p...ight=recursion
    Last edited by Tinbeard; Aug 16th, 2007 at 03:55 PM.
    If my post helps , please feel free to rate it

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