473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

treeview problem with collection


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

Nov 20 '05 #1
6 1494
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 ;)
"Jan Krouwer" <ja*********@comcast.net> wrote in message
news:nzOkb.825705$YN5.855111@sccrnsc01...

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

Nov 20 '05 #2
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.

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:bn*******************@news.demon.co.uk...
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 ;)
"Jan Krouwer" <ja*********@comcast.net> wrote in message
news:nzOkb.825705$YN5.855111@sccrnsc01...

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


Nov 20 '05 #3
With Deft Fingers, "Jan Krouwer" <ja*********@comcast.net> wrote:
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
Nov 20 '05 #4
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


Nov 20 '05 #5
Actually, the posted code has some problems - when tested for more cases -
sorry

"Jan Krouwer" <ja*********@comcast.net> wrote in message
news:HH7lb.842424$uu5.149883@sccrnsc04...
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

Nov 20 '05 #6
With Deft Fingers, "Jan Krouwer" <ja*********@comcast.net> wrote:
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
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
3
by: feel | last post by:
Goin' crazy with this recursive function ported from delphi... I send a string like DirA/ DirB /DirC but i get in the treeView each one in a new node.Cant get the child node....!! -DirA -DirB...
4
by: Chuck Bowling | last post by:
I have a serialized TreeNodeCollection that I want to initialize a TreeView with. Is there a simple assignment I can use for this or do I have to iterate thru the collection and add individual...
2
by: Chris | last post by:
Hi, anyone experience with the TreeView control that is available in the Microsoft.Web.UI.WebControls component ? I want to add a few nodes to the TreeView-control but don't know quite how :...
12
by: Bob Hollness | last post by:
Hi. I have a text file containing a list of files and their path. I.e. c:\MyDocs\File1.txt c:\MyDocs\File2.txt c:\MyDocs\File3.txt c:\MyDocs\File4.txt C:\MyDocs\Folder\File1.txt
1
by: garyusenet | last post by:
Hi All, I'm eagerly anticipating what you have to say on something that's stumped me over the weekend. I'm writing a small application which utilises a treeview control. I've figured out how to...
1
by: | last post by:
What's the best way to create new nodes on a treeview 2.0 and save them to an xml file? Thanks, Victor
4
by: John Dann | last post by:
I'm trying to implement a fairly simple Treeview procedure but running into problems. My Treeview has just two levels of nodes: top-level and one child level. I want a doube-click on the...
6
by: balurajeev84 | last post by:
Hi, I am trying to convert a VB.net code to C# . it is to bind a treeview from a collection. but not finding a Collection which stores treenodes as object. Cant use hashtable since it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.