|
-
Aug 16th, 2007, 05:47 AM
#1
Thread Starter
Hyperactive Member
[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:
Public Class Categories
Dim dt As DataTable
Dim dr As DataRow
Dim tn As TreeNode
Private Sub Categories_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dt = Main.dtCats
'if the category list is still being downloaded show the please wait message.
lblMessage.Visible = True
trvCats.ImageList = ImageList1
End Sub
Private Sub Categories_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
If dt.Rows.Count > 1 Then
loadLIst()
End If
End Sub
'///
'This method loads the initial top level parents and any associated child items
'///
Public Sub loadList()
'Hide the please wait message
lblMessage.Visible = False
For Each dr In dt.Rows
If dr("level") = "1" Then
tn = New TreeNode
tn.Text = dr("name")
tn.Name = dr("CatID")
'check if the node is a leaf category, if so set the images required
If dr("leaf") Then
tn.ImageIndex = 2
tn.SelectedImageIndex = 2
End If
trvCats.Nodes.Add(tn)
AddChildNodes(tn)
End If
Next
End Sub
'///
'This method add the child nodes to the parent node
'///
Private Sub AddChildNodes(ByVal tn As TreeNode)
If tn.Nodes.Count = 0 Then
Dim childNode As TreeNode
For Each dr As DataRow In dt.Rows
'find the rows that have the same parent ID but not the same name
'as this will add a duplicate of the parent category to the list
If dr("ParentID") = tn.Name AndAlso dr("name") <> tn.Text Then
childNode = New TreeNode
childNode.Text = dr("name")
childNode.Name = dr("CatID")
'if the category is a leaf then add the appropriate image
If dr("leaf") Then
childNode.ImageIndex = 2
childNode.SelectedImageIndex = 2
End If
'add the node to the collection
tn.Nodes.Add(childNode)
End If
Next
End If
End Sub
Private Sub trvCats_AfterCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvCats.AfterCollapse
'node has been collapsed so reset the image displayed
e.Node.ImageIndex = 0
End Sub
Private Sub trvCats_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvCats.AfterSelect
'get the id of the selected node
tn = e.Node
AddChildNodes(tn)
For Each node As TreeNode In tn.Nodes
AddChildNodes(node)
Next
End Sub
Private Sub trvCats_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles trvCats.BeforeExpand
'get the id of the selected node
tn = e.Node
'node is being expanded so change the image displayed
tn.ImageIndex = 1
AddChildNodes(tn)
For Each node As TreeNode In tn.Nodes
AddChildNodes(node)
Next
End Sub
End Class
If my post helps , please feel free to rate it 
-
Aug 16th, 2007, 07:32 AM
#2
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.
-
Aug 16th, 2007, 07:41 AM
#3
Fanatic Member
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.
-
Aug 16th, 2007, 07:42 AM
#4
Thread Starter
Hyperactive Member
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 
-
Aug 16th, 2007, 09:15 AM
#5
Thread Starter
Hyperactive Member
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 
-
Aug 16th, 2007, 01:45 PM
#6
Fanatic Member
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.
-
Aug 16th, 2007, 03:45 PM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|