Treeview control in VB.NET

R

Rich

Hi

I am trying to setup a treeview control in VB.NET using windows forms. I
have a table which looks similar to this:

Site Building Location
London Main Office 1st Floor
London Main Office 2nd Floor
London Workshop Office
Newcastle Office 1 1st Floor
Newcastle Office 1 2nd Floor
Newcastle Office 2 1st Floor

I would like to use the data from this table which I have imported to a
datatable to display a treeview which looks like this:

London -|
|-Main Office-|
| |-1st Floors
| |-2nd Floor
|Workshop-|
| |-Office
Newcastle-|
|-Office 1-|
| |-1st Floor
| |-2nd Floor
|-Office 2-|
|-1st Floor


Sorry the diagram is not very good, but hopefully you will understand what I
want. Does anyone know how I can code this in VB as I understand how the
treeview works but can't work out how to create the child nodes from the
table?

Many thanks
Rich
 
C

Cor

Hi Richard,

I have more samples, does this fullfil your question?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim l As Integer() = {0, 0, 1, 0, 1, 5}
Dim t As New TreeNode("Root")
TreeView1.Nodes.Add(t)
For i As Integer = 1 To 5
Dim tn As New TreeNode
tn = t.Nodes.Add("Root\Node" & i.ToString & "\")
Dim sn As New TreeNode
For j As Integer = 1 To l(i)
sn = tn.Nodes.Add("Root\Node" & i.ToString & "\SubNode\" & _
i.ToString & j.ToString)
Dim sn2 As New TreeNode
For h As Integer = 1 To l(6 - j)
sn2 = sn.Nodes.Add("Root\Node" & i.ToString &
"\SubNode\" & _
i.ToString & j.ToString & "\" _
& "SubNode" & i.ToString & j.ToString & h.ToString)
Next
Next
Next
End Sub
//
 
A

Ayaz Ahmed

Hello,

here is my visual basic code , because i dont have much time to covert
into vb.net

i hope its help you

'*******************************TVAccountsHierarchy_NodeClick()*********
*********************
'When the node in ther tree is clicked the value in different
Combo/textboxes will be changed as well
'to reflect the change in the tree's node in the CHART OF ACCOUNTS tab

Private Sub TVAccountsHierarchy_NodeClick(ByVal Node As
MSComctlLib.Node)
Dim strsql As String 'the source for the recordset
Dim objrsVouchers As ADODB.Recordset 'for Accounts table

Set objrsVouchers = New ADODB.Recordset
strsql = "Select * from Accounts where Name = '" & Node.Text & "'"

objrsVouchers.Open strsql, objconn 'Opent the Accounts table

If objrsVouchers.EOF = True Then 'if the record not found
MsgBox "The Account's details cannot be found in the ACCOUNTS
table.", vbInformation, "Alert"
EOFFlag = 1 'the EOF is REached
GoTo errCA 'the end of this function
End If

EOFFlag = 0 'reset the EOF flag/ The record is found somewhere

MaskCACode.Text = objrsVouchers.Fields("Code") 'display the details
of the first record
cmbCACode.Text = objrsVouchers.Fields("Code") 'display the details
of the first record
cmbCAName.Text = objrsVouchers.Fields("Name")
txtCADescription.Text = objrsVouchers.Fields("Description")
txtCAType.Text = objrsVouchers.Fields("Type")
cmbCANature = objrsVouchers.Fields("Nature")
txtCACurrentBalance.Text = objrsVouchers.Fields("Current_Balance")
txtCAOpeningBalance.Text = objrsVouchers.Fields("Opening_Balance")

errCA:
objrsVouchers.Close 'close the table
Set objrsVouchers = Nothing
'Node.Text

End Sub


'*********************************************Form_Load*****************
****************
'This function will be called at the strting of the module
'a connection is established to access the database. The default tab is
set to 1
Private Sub Form_Load()
frmAccountingMain.MousePointer = 11
SSTabAccountAsli.Tab = 0 'CHART OF ACCOUNTS is the current
tab

SSTabAccountAsli.TabEnabled(2) = False

Dim objrsTreeroot As ADODB.Recordset 'these are needed to form
the structure of the tree from the database
Dim objrsTreeBranch As ADODB.Recordset
Dim objrsTreeLeaf As ADODB.Recordset
Dim objrsTreeLeafLeaf As ADODB.Recordset

Dim strsql As String 'the source of the recordset
Dim TreeLevel2() As String 'the strings for the leaves(LEVEL2) of the
tree
Dim nodeCode As Node 'the node to be added to the tree
Dim total As Integer 'how many items are in a current level of
tree
Dim I As Integer 'loop variable

Set objconn = New ADODB.Connection
objconn.Open ModuleConnectionString.ConnectionString

Dim objrsAccounts As ADODB.Recordset 'for Accounts table
Set objrsAccounts = New ADODB.Recordset

objrsAccounts.Open "Select * from Accounts", objconn 'Opent the
Accounts table

'load the first two combo boxes with the entries in the databases

If objrsAccounts.EOF = True Then 'if the Accounts table is empty
MsgBox "The ACCOUNTS Table is empty add some records.",
vbExclamation, "Alert"
EOFFlag = 1 'the EOF is REached
GoTo errCA 'the end of this function
End If

EOFFlag = 0 'reset the EOF flag/ The table is not empty

While objrsAccounts.EOF <> True 'go until the EOF is reached
cmbCACode.AddItem objrsAccounts.Fields("Code") 'fill the 2 combos
cmbCAName.AddItem objrsAccounts.Fields("Name")
objrsAccounts.MoveNext 'move and fill the combos with the
next record
Wend

objrsAccounts.MoveFirst 'move to the first record in the table

cmbCACode.ListIndex = 0 'display the first record code in
combo
cmbCAName.ListIndex = 0 'display the first record Name in
combo
cmbCACode.Text = objrsAccounts.Fields("Code") 'display the details
of the first record
cmbCAName.Text = objrsAccounts.Fields("Name")
txtCADescription.Text = objrsAccounts.Fields("Description")
txtCAType.Text = objrsAccounts.Fields("Type")
cmbCANature.Text = objrsAccounts.Fields("Nature")
txtCACurrentBalance.Text = objrsAccounts.Fields("Current_Balance")
txtCAOpeningBalance.Text = objrsAccounts.Fields("Opening_Balance")

Set objrsTreeroot = New ADODB.Recordset 'Set all the recordsets for
the formation of the tree
Set objrsTreeBranch = New ADODB.Recordset
Set objrsTreeLeaf = New ADODB.Recordset
Set objrsTreeLeafLeaf = New ADODB.Recordset

'FORMATION OF TREE STARTS HERE
'Select the LEVEL-1 account heads for the root
of the tree
strsql = "Select * from Accounts where Account_Level=1 order by Code"

objrsTreeroot.Open strsql, objconn 'open the Roots node

While objrsTreeroot.EOF <> True 'Make the roots of the tree to the
records whose account_level is 1
'add the name to the root of the tree make
the uppercase of account_name as the key for this node
TVAccountsHierarchy.Nodes.Add , ,
UCase(objrsTreeroot.Fields("Name")), objrsTreeroot.Fields("Name")
'Select the level_2 accounts node to form the branches
of the tree
strsql = "Select * from Accounts where Account_Level=2 and Type= '"
& objrsTreeroot.Fields("Name") & "' order by Code"
'Retrive the branches from the table
objrsTreeBranch.Open strsql, objconn
'add all the names to the Tree' node as the Branch of
the tree whose account level is 2
While objrsTreeBranch.EOF <> True
'add this node as the tree branch make it's root to the
Node which is the current Root node and it's Key as it's uppercase name
TVAccountsHierarchy.Nodes.Add
UCase(objrsTreeroot.Fields("Name")), tvwChild,
UCase(objrsTreeBranch.Fields("Name")), objrsTreeBranch.Fields("Name")
'Select the level_3 accounts node to form the
leaves of the tree
strsql = "Select * from Accounts where Account_Level=3 and
Type= '" & objrsTreeBranch.Fields("Name") & "' order by Code"
'Retrive the trees from the table
objrsTreeLeaf.Open strsql, objconn
'add all the names to the Tree' node as the leaf of
the tree whose account level is 3
While objrsTreeLeaf.EOF <> True
'add this node as the tree leaf make it's root to
the Node which is the current branch node and it's Key as it's uppercase
name
TVAccountsHierarchy.Nodes.Add
UCase(objrsTreeBranch.Fields("Name")), tvwChild,
UCase(objrsTreeLeaf.Fields("Name")), objrsTreeLeaf.Fields("Name")
'the 4th level strsql
strsql = "Select * from Accounts where Account_Level=4
and Type= '" & objrsTreeLeaf.Fields("Name") & "' order by Code"
objrsTreeLeafLeaf.Open strsql, objconn
'add all the names to the Tree' node as the leaf of
the tree whose account level is 3
While objrsTreeLeafLeaf.EOF <> True
TVAccountsHierarchy.Nodes.Add
UCase(objrsTreeLeaf.Fields("Name")), tvwChild,
UCase(objrsTreeLeafLeaf.Fields("Name")),
objrsTreeLeafLeaf.Fields("Name")
objrsTreeLeafLeaf.MoveNext
Wend

objrsTreeLeafLeaf.Close
objrsTreeLeaf.MoveNext 'form the next leaf for the
current Branch( UCase(objrsTreeBranch.Fields("Name"))) and for the
current root( UCase(objrsTreeroot.Fields("Name")))
Wend
objrsTreeLeaf.Close 'leaves for this branch and root are
finished

objrsTreeBranch.MoveNext 'form the next branch for the
current root( UCase(objrsTreeroot.Fields("Name")))
Wend

objrsTreeBranch.Close 'branches for this root are finished
objrsTreeroot.MoveNext 'form the next root for the tree
Wend

objrsTreeroot.Close 'close the table
Set objrsTreeroot = Nothing
Set objrsTreeBranch = Nothing
Set objrsTreeLeaf = Nothing
Set objrsTreeLeafLeaf = Nothing

errCA:
objrsAccounts.Close 'close the table
Set objrsAccounts = Nothing

frmAccountingMain.MousePointer = 0

End Sub


Thanks,



Warm Regards,

Ayaz Ahmed
Software Engineer & Web Developer
Creative Chaos (Pvt.) Ltd.
"Managing Your Digital Risk"
http://www.csquareonline.com
Karachi, Pakistan
Mobile +92 300 2280950
Office +92 21 455 2414
 
R

Rich

Hi Cor,

That pointed me in the right direction and I have the treeview working now.

Many thanks
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top