Create Treeview from Datatable using

In this article we will learn that how to create TreeView control from datatable object in language.

This example requires one windows form with TreeView control named TreeView1.
First we create a datatable object and then we will bind treeview with node like this:

Bind treeview control with datatable

Public Function CreateDataTable() As DataTable
        Dim dataTable As New DataTable()
        ' The value in this column will display on the TreeNode
        ' The value in this column will identify its parentId 
        ' Fill the DataTable
        dataTable.Rows.Add("India", "New Delhi")
        dataTable.Rows.Add("India", "Mumbai")
        dataTable.Rows.Add("India", "Kolkata")
        dataTable.Rows.Add("India", "Noida")
        dataTable.Rows.Add("USA", "New York")
        dataTable.Rows.Add("USA", "Washington")
        dataTable.Rows.Add("USA", "")
        dataTable.Rows.Add("USA", "India")
        Return dataTable
    End Function

above datatable has data like this

Treeview after binding from datatable in

And now we will create TreeView from above datatable. This example also learn that how to create TreeView at runtime or how to bind TreeView nodes to TreeView control at runtime.

    Public Sub BuildTree(ByVal dt As DataTable, ByVal trv As TreeView, ByVal expandAll As [Boolean])
        ' Clear the TreeView if there are another datas in this TreeView
        Dim node As TreeNode
        Dim subNode As TreeNode
        For Each row As DataRow In dt.Rows
            'search in the treeview if any country is already present
            node = Searchnode(row.Item(0).ToString(), trv)
            If node IsNot Nothing Then
               'Country is already present
                subNode = New TreeNode(row.Item(1).ToString())
                'Add cities to country
                node = New TreeNode(row.Item(0).ToString())
                subNode = New TreeNode(row.Item(1).ToString())
                'Add cities to country
            End If
        If expandAll Then
            ' Expand the TreeView
        End If
    End Sub
    Private Function Searchnode(ByVal nodetext As String, ByVal trv As TreeView) As TreeNode
        For Each node As TreeNode In trv.Nodes
            If node.Text = nodetext Then
                Return node
            End If
    End Function
  • Chris

    Excellent! Thank you! This has explained and helped me understand the syntax much better than the microsoft example, which requires you use a dataarray for storing the indexes of Parent Nodes. I was thinking though, wouldn’t a Dictionary be quicker and more efficient for that rather than iterating through every node with the search function each time?

  • Very useful. Perhaps you might consider talking about the opposite task as well: saving a TreeView into a datatable.

    • Thank you Tim,

      I have added your suggestion in my todo list. I will notify you when it will be available.

  • Malcolm Smart

    FYI, your ‘Searchnode’ function doesn’t return a value on all of its code paths.

  • Shawn_0816

    Hi, I want to know if it could work for more layers of the tree, not only two layers