treeview problem with collection

  • Thread starter Thread starter Jan Krouwer
  • Start date Start date
J

Jan Krouwer

I have a treeview which is populated from a relational database. In order to
copy part of the tree, I need to add to the database the relationship of the
part of the tree to be copied but with new ids. I have built a collection of
the right number of ids. I am using the following code to recursively go
through the tree (some code left out). Pos is the position in the collection
of ids - I need to select the correct ids (need to use previous ids) to
maintain the node relationship which is done by testing node properties.

What is strange is that pos is the correct number in this routine but when
the last node is reached, pos changes to the wrong number. Any ideas or
another way to add a hierarchical relationship to a database from a set of
treeview nodes.

Private Sub Copy(ByVal n As TreeNode, ByVal pos As Integer)
Dim aNode As TreeNode

pos=pos+1

For Each aNode In n.Nodes
Copy(aNode, pos)
Next
End Sub

' Call the procedure using the top nodes of the treeview.
Private Sub CopyStart(ByVal aTreeView As TreeView)
Dim n As TreeNode
For Each n In aTreeView.Nodes
Copy(n,1)
Next
End Sub
 
I'm not sure what your "Start" routine is doing here - do you want all of
your top level nodes to start off with the value "1"? If you want Unique
IDs then this is wrong, however, I'm guessing you want to label nodes by
their depth, in which case, its correct. When you say "pos is the position
in the collection of Ids" I get confused. What is the collection of Ids?
If pos is supposed to be a unique position, then Pos should be incremented
within the loops, not outside of them.

More information required I guess ;)
 
Thanks for your help

Rather than trying to figure this out, I abandoned recursion and changed the
routine to a Do While loop which works fine (has a test for firstnode to see
if child nodes are present). I have two of these loops. One is to build a
collection of unique ids (Guids) and the second loop is to get the data from
the database and change the ids (an event id and parent id) as required to
maintain the hierarchical relationship of the nodes and the fact that an
event id must be unique.
 
Jan Krouwer said:
Rather than trying to figure this out, I abandoned recursion and changed the
routine to a Do While loop which works fine (has a test for firstnode to see
if child nodes are present). I have two of these loops. One is to build a

Boy I'd like to see that code (:

I've been bashing my head on Recursion without success. I've a 3d array with
parent/child info that I'm trying to create a treeview (the info quantities
vary... but never reach any excessive quanitity... say less than 30 total).

My parents and childs are 3 numbers (ie: 010-020 = parent-child). In my array
the parent and child are unique inputs.

Regards,

Bruce
 
Bruce,

Here is the code, with lengthy database code omitted.

Jan

Dim pos As Integer = 1

Dim ChildNode As TreeNode



'Routine starts at clicked node in treeview named TV

Dim SNode As TreeNode = TV.SelectedNode



'Check to make sure node was clicked

If IsNothing(SNode) Then

MessageBox.Show("You must select a node first")

Exit Sub

End If



'reset id collection

If Not IsNothing(AllNewEventIDs) Then AllNewEventIDs = Nothing

AllNewEventIDs = New Collection



Cursor.Current = Cursors.WaitCursor



'next code - not shown - deletes data from a table used to hold
copy information

'it is standard ado.net but would make this message long



'create unique id for starting node

Dim firsttime As Boolean = True

If firsttime = True Then

Dim EventGuid As Guid = Guid.NewGuid

AllNewEventIDs.Add(EventGuid.ToString)

End If

firsttime = False



'create unique id for all child nodes of parent

Do While Not IsNothing(SNode.FirstNode)

For Each ChildNode In SNode.Nodes

Dim EventGuid As Guid = Guid.NewGuid

AllNewEventIDs.Add(EventGuid.ToString)

Next ChildNode

'done with set of child nodes - set current node to last child
node to see if it has parents

SNode = ChildNode

'test if there are more children

If IsNothing(SNode.FirstNode) Then

Exit Do

End If

Loop



'reset starting conditions for second loop

SNode = TV.SelectedNode

firsttime = True



'most of next code - not shown - gets data from a table that has
node values stored in database

'then puts this data into a table that is later used in paste
operation

'it is standard ado.net but would make this message long

'code fragments left here shows how hierarachical relationship is
maintained



Dim numSib As Integer = 1

If firsttime = True Then



'get the data using SNode.tag in query which is event_id

'add same record to Temp table but give it new event_id

'parent_id for first record not important since it is always
changed in paste operation



dr("Event_Id") = AllNewEventIDs.Item(pos)

dr("Parent_ID") = "Dummy"



'increment position in collection

pos = pos + 1



End If

firsttime = False



Do While Not IsNothing(SNode.FirstNode)

For Each ChildNode In SNode.Nodes



'get the data for the rest of nodes using SNode.tag in query
which is event_id

'add same record to Temp table but give it new event_id

'parent_id depends on how many children



'get event_ID from collection

dr("Event_Id") = AllNewEventIDs.Item(pos)

dr("Parent_ID") = AllNewEventIDs.Item(pos - numSib)



pos = pos + 1

numSib = numSib + 1

Next ChildNode



'test if there are more children

SNode = ChildNode

numSib = 1

If IsNothing(SNode.FirstNode) Then

Exit Do

End If

Loop

Cursor.Current = Cursors.Default
 
Jan Krouwer said:
Actually, the posted code has some problems - when tested for more cases -
sorry

Oh... Ok... but thanks anyways. I might find something useful to my case
anyways (always nice seeing others code regardless).

Regards,

Bruce
 
Back
Top