Recursive Treeview in vb.net

I needed to do a recursive treeview which didnt  have a set number of levels (ragged) in an application I work on at my employer. Hope this example helps some people.


Join me on Facebook

 
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = GetDatatable()
        Dim tree As New MyNode(0, "root", 0)
        Do While dt.Rows.Count > 0
            For i As Integer = dt.Rows.Count – 1 To 0 Step -1
                Dim row As DataRow = dt.Rows(i)
                If tree.AddNode(New MyNode(CInt(row("PoolID")), CStr(row("PoolName")), CInt(row("PoolParent")))) Then
                    dt.Rows.Remove(row)
                End If
            Next
        Loop
        For Each node As MyNode In tree.ChildNodes
            LoadTree(node, TreeView1.Nodes)
        Next
    End Sub

    Private Sub LoadTree(ByVal n As MyNode, ByVal nodes As TreeNodeCollection)

        Dim tvn As TreeNode = nodes.Add(n.Name)
        For Each child As MyNode In n.ChildNodes
            LoadTree(child, tvn.Nodes)
        Next

    End Sub

    Private Function GetDatatable() As DataTable
        Dim dt As New DataTable("tree")
        dt.Columns.Add("PoolID", GetType(Integer))
        dt.Columns.Add("PoolName", GetType(String))
        dt.Columns.Add("PoolParent", GetType(Integer))
        dt.PrimaryKey = New DataColumn() {dt.Columns("PoolID")}
        dt.Rows.Add(1, "N4", 3)
        dt.Rows.Add(2, "N2", 0)  ‘root node, null parent
        dt.Rows.Add(3, "N3", 4)
        dt.Rows.Add(4, "N1", 0)  ‘root node, null parent
        dt.Rows.Add(5, "N5", 3)
        Return dt

    End Function
End Class
Public Class MyNode

    Public Sub New(ByVal xID As Integer, ByVal xName As String, ByVal xParentID As Integer)
        Name = xName
        ID = xID
        ParentID = xParentID
    End Sub
    Public ChildNodes As New List(Of MyNode)
    Public Name As String
    Public ID As Integer
    Public ParentID As Integer

    Public Function AddNode(ByVal node As MyNode) As Boolean
        Dim Ret As Boolean = False
        If Me.ID = node.ParentID Then
            ChildNodes.Add(node)
            Ret = True
        Else
            For Each child As MyNode In ChildNodes
                If child.AddNode(node) Then
                    Ret = True
                End If
            Next
        End If
        Return Ret
    End Function

End Class


Join me on Facebook

Technorati Tags: ,,,,,,,,,,,,,,,,

Windows Live Tags: vb.net,.NET Framework,csharp,Recursive,Treeview,DataTable,tree,node,ChildNodes,LoadTree,Nodes,TreeNodeCollection,TreeNode,Name,DataColumn,parent,Columns

Advertisements
  1. #1 by Ray on November 9, 2012 - 11:50 pm

    This is nice – works well. Any thoughts on sorting the nodes into alpha-numberic order?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: