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

  1. Public Function CreateDataTable() As DataTable
  2.         Dim dataTable As New DataTable()
  3.         dataTable.Columns.Add("Country")
  4.         ' The value in this column will display on the TreeNode
  5.         dataTable.Columns.Add("City")
  6.         ' The value in this column will identify its parentId
  8.         ' Fill the DataTable
  9.         dataTable.Rows.Add("India", "New Delhi")
  10.         dataTable.Rows.Add("India", "Mumbai")
  11.         dataTable.Rows.Add("India", "Kolkata")
  12.         dataTable.Rows.Add("India", "Noida")
  13.         dataTable.Rows.Add("USA", "New York")
  14.         dataTable.Rows.Add("USA", "Washington")
  15.         dataTable.Rows.Add("USA", "")
  16.         dataTable.Rows.Add("USA", "India")
  17.         Return dataTable
  18.     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.

  1.     Public Sub BuildTree(ByVal dt As DataTable, ByVal trv As TreeView, ByVal expandAll As [Boolean])
  2.         ' Clear the TreeView if there are another datas in this TreeView
  3.         trv.Nodes.Clear()
  4.         Dim node As TreeNode
  5.         Dim subNode As TreeNode
  6.         For Each row As DataRow In dt.Rows
  7.             'search in the treeview if any country is already present
  8.             node = Searchnode(row.Item(0).ToString(), trv)
  9.             If node IsNot Nothing Then
  10.                'Country is already present
  11.                 subNode = New TreeNode(row.Item(1).ToString())
  12.                 'Add cities to country
  13.                 node.Nodes.Add(subNode)
  14.             Else
  15.                 node = New TreeNode(row.Item(0).ToString())
  16.                 subNode = New TreeNode(row.Item(1).ToString())
  17.                 'Add cities to country
  18.                 node.Nodes.Add(subNode)
  19.                 trv.Nodes.Add(node)
  20.             End If
  21.         Next
  22.         If expandAll Then
  23.             ' Expand the TreeView
  24.             trv.ExpandAll()
  25.         End If
  26.     End Sub
  2.     Private Function Searchnode(ByVal nodetext As String, ByVal trv As TreeView) As TreeNode
  3.         For Each node As TreeNode In trv.Nodes
  4.             If node.Text = nodetext Then
  5.                 Return node
  6.             End If
  7.         Next
  8.     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