Beruflich Dokumente
Kultur Dokumente
santry.com
Patrick Santry
In this tutorial we'll describe how to populate a treeview control on a Windows form using VB.Net. This
code is useful where you have a parent child relationship contained within the same table. For example say
you have the following table structure:
RecordID
1 0 Topic 1
2 0 Topic 2
3 1 RE: Topic 1
4 1 RE: Topic 1
5 2 RE: RE: Topic 1
6 2 RE: RE: Topic 1
You can see from the above table that we have several records, some of
the records have a 0 as a ParentID, meaning this is a top level parent
record, then other records do have a value other than 0 in the ParentID, meaning they are children of the
record that has a matching ID in the table.
This structure is very versatile, in that you can have unlimited child
records using this structure, thus allowing you to create as many nests or branches you wish.
In the following routine we have a recursive sub routine which calls itself until there are no more records.
The purpose of this is to populate our treeview control with the parent child records.
First in the Form_Load routine we create a root node for our tree and
pass this root node and parent id of the record, in this example we'll pass the number 0, which will give us
all the parent level nodes in the table.
1 of 4 25-Jan-17 16:31
Populating a .Net Treeview control from a parent-child relationship db t... about:reader?url=http://www.santry.com/Blog/tabid/90/ID/1241/Popula...
TreeView1.Nodes.Clear()
tNode = TreeView1.Nodes(0)
PopulateTreeView(0, tNode)
tNode.Expand()
End Sub
Then in PopulateTreeView routine we pass the parent id of the record and the node of the tree that we
are currently in. This routine will call itself to see if there are any child records of the record we're
currently in.
2 of 4 25-Jan-17 16:31
Populating a .Net Treeview control from a parent-child relationship db t... about:reader?url=http://www.santry.com/Blog/tabid/90/ID/1241/Popula...
CNASPSearch.Open()
DACategories.Fill(DSNASPSearch, "ASPSearch_Categories" )
CNASPSearch.Close()
inTreeNode.Nodes.Add(parentnode)
'set the tag property for the current node. This comes in
useful if
'FillDataGrid(TreeView1.SelectedNode.Tag)
3 of 4 25-Jan-17 16:31
Populating a .Net Treeview control from a parent-child relationship db t... about:reader?url=http://www.santry.com/Blog/tabid/90/ID/1241/Popula...
parentnode.Tag = parentrow.Item(0)
PopulateTreeView(parentrow.Item(0), parentnode)
Next parentrow
End Sub
Continue on to Part II - Populating the Grid Control from the Treeview Node Selection.
Patrick Santry, has two decades of experience in enabling businesses to take advantage of the digital
landscape. A well rounded experience in technology, and business is what sets me apart from the rest of the
pack. When it comes to an overall digital strategy my experience is impressive.
BS in Computer Information Systems. Four time recipient of the Microsoft MVP Award, and author of
several books and magazine articles on digital technologies.
4 of 4 25-Jan-17 16:31