Create Treeview from Datatable using vb.net

 
In this article we will learn that how to create TreeView control from datatable object in vb.net 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
  7.  
  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 vb.net

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
  1.  
  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

facebooktwittergoogle_plusredditpinterestlinkedinmail