Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

treeview nodes from database

Status
Not open for further replies.

buddyel

MIS
Mar 3, 2002
279
US

I am trying to configure a treeview on my form to retrieve its node listing from an Access database. This is how the database is configured...

ID PARENT_ID DESCRIPTION
1 0 CORPORATE DOCUMENTS
2 1 QUALITY CONTROL
3 1 PRODUCTION
4 1 ENGINEERING
5 1 ACCOUNTING
6 1 SALES
7 2 QUALITY POLICIES
8 2 QUALITY PROCDURES
9 4 RETURNED GOODS

This is how the treeview nodes should appear...

Corporate Documents
+ Quality Control
- Quality Policies
- Quality Procedures
+ Production
+ Engineeering
- Returned Goods
+ Accounting
+ Sales

I have no problem getting the description field into the treeview but how can i categorize them ?? thanks in advance.
 
I have come up with this much of the code so far. It creates the root nodes fine, but i am not sure how to code it so it pulls up any child nodes and inserts them under the appropriate parent node. i got it to work for just one node, any ideas?

TreeView1.Nodes.Insert(0, New TreeNode("Corporate Documents"))
Do While dr.Read
If dr.Item("PARENT_ID") = 1 Then
TreeView1.Nodes.Add(New TreeNode(dr.Item("Category")))
End If
If dr.Item("PARENT_ID") = 2 Then
Dim FirstNode As TreeNode = TreeView1.Nodes(1)
FirstNode.Nodes.Insert(1, New TreeNode(dr.Item("Category")))
End If
Loop
 

I found an example from microsoft which is pretty much what i want but it is in vb6 and i am unsure on how to code it for vb.net. Here is what it looks like...

The database looks like this...

Key Parent Text image selectedimage
1_ 0_ Last 1_ 1 2
2_ 1_ Child 3_ 1 2
3_ 1_ Child 4_ 1 2
4_ 0_ Last 2_ 1 2
5_ 2_ Child 5_ 1 2

The code looks like this......

Do While mRS.EOF = False
nImage = mRS.Fields("image")
nSelectedImage = mRS.Fields("selectedimage")
if Trim(mRS.Fields("parent")) = "0_" Then ' All root nodes have 0_ in the parent field
Set oNodex = TreeView1.Nodes.Add(, 1, Trim(mRS.Fields("key")), _
Trim(mRS.Fields("text")), nImage, nSelectedImage)
Else ' All child nodes will have the parent key stored in the parent field
Set oNodex = TreeView1.Nodes.Add(Trim(mRS.Fields("parent")), tvwChild, _
Trim(mRS.Fields("key")), Trim(mRS.Fields("text")), nImage, nSelectedImage)
oNodex.EnsureVisible ' expend the TreeView so all nodes are visible
End if
mRS.MoveNext
Loop
End if

In VB6 the syntax for the TreeView.Nodes.Add = TreeView.Nodes.Add(Relative,Relationship,key,Text,Image,SelectedImage)
in VB.Net the syntax is TreeView.Nodes.Add(text as String) so i am not sure how to convert, so if anyone can help i would really appreciate it. thanks
 

just trying to bump this back to the top of the board.... any ideas anyone?
 
I have had to do something similar to this. This may or may not work for you.

Structure treeitems
Dim tr as treenode
Dim parent as integer
End structure

Dim Treeitem() Treeitems

Public sub AddTreeNode(...insert the info to transfer to the node here.... )
Dim count as integer
count = treeitems.getlength(0)
Redim Preserve Treeitem(count)
treeitem(count).tr = new treenode
treeitem(count).text = text
addTreeItems
End Sub

Public Sub RefreshTreeItems
Treeview1.nodes.clear
Dim i as integer
For i = 0 to treeitems.GetLength(0) -1
if treeitem(i).Parent = 0 then
Treeview1.nodes.add(treeitem(i).tr)
else
treeitem(treeitem(i).Parent).tr.Nodes.add(treeitem(i).tr)
Next
End Sub


So you would call the sub AddTreeNode and pass the info you want to assign to the node like text, tag, color..... and it should add it to the tree and sort it accordingly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top