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