)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to populate WinForms TreeViewAdv from datatable?

Platform: WinForms |
Control: TreeViewAdv |
Published Date: September 13, 2017 |
Last Revised Date: February 4, 2020 |
.NET Framework: 4

Populate the TreeViewAdv

In TreeViewAdv there is no default support to bind DataTable and it can be achieved by loading TreeNodeAdv in iteration process from DataTable. This can be done by following below steps.

1. Need to initialize the DataTable with required data in Columns and Rows combination.

2. Need to integrate DataTable in DataSet and define Parent Child relationship.

3. Need to create TreeNodeAdv in iteration process from DataTable and load it in TreeViewAdv.

C#

// DataTable
DataTable dt = new DataTable("AddressLists");
 
//Use a DataSet to manage the data
DataSet ds = new DataSet();
 
//Add columns to the DataTable
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Parent", typeof(string));
 
//Add rows to the DataTable
dt.Rows.Add(new string[] { "Root", "0" });
dt.Rows.Add(new string[] { "Work", "Root" });
dt.Rows.Add(new string[] { "Home", "Root" });
dt.Rows.Add(new string[] { "One", "Work" });
dt.Rows.Add(new string[] { "Two", "Work" });
dt.Rows.Add(new string[] { "One", "Home" });
dt.Rows.Add(new string[] { "Two", "Home" });
dt.Rows.Add(new string[] { "Three", "Home" });
dt.Rows.Add(new string[] { "Other", "Root" });
 
//Add DataTable to the DataSet
ds.Tables.Add(dt);
//Add a relationship
ds.Relations.Add("TreeParentChild", ds.Tables["AddressLists"].Columns["Name"],
ds.Tables["AddressLists"].Columns["Parent"], false);
 
/// <summary>
/// To Populate TreeView
/// </summary>
public void PopulateTree(DataRow dr, TreeNodeAdv pNode)
{
  //To iterate through all the rows in the DataSet
  foreach (DataRow row in dr.GetChildRows("TreeParentChild"))
  {
    //Creating a TreeNode for each row
    TreeNodeAdv cChild = new TreeNodeAdv(row["Name"].ToString());
    //Add cChild node to the pNode
    pNode.Nodes.Add(cChild);
    //Recursively build the tree
    PopulateTree(row, cChild);
  }
}
 
/// <summary>
/// Button Click event
/// </summary>
private void buttonAdv1_Click(object sender, EventArgs e)
{
  //Iterate all the rows in the DataSet
  foreach (DataRow dr in ds.Tables["AddressLists"].Rows)
  {
    if(dr["Parent"].ToString() == "0")
    {
      //Creates a TreeNode if the parent equals 0
      TreeNodeAdv root = new TreeNodeAdv(dr["Name"].ToString());
      treeViewAdv1.Nodes.Add(root);
      //Recursively builds the tree
      PopulateTree(dr, root);
    }
  }
  //Expands all the tree nodes
  treeViewAdv1.ExpandAll();
}

VB

' DataTable
Private dt As New DataTable("AddressLists")
 
'Use a DataSet to manage the data
Private ds As New DataSet()
 
'Add columns to the DataTable
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Parent", GetType(String))
 
'Add rows to the DataTable
dt.Rows.Add(New String() { "Root", "0" })
dt.Rows.Add(New String() { "Work", "Root" })
dt.Rows.Add(New String() { "Home", "Root" })
dt.Rows.Add(New String() { "One", "Work" })
dt.Rows.Add(New String() { "Two", "Work" })
dt.Rows.Add(New String() { "One", "Home" })
dt.Rows.Add(New String() { "Two", "Home" })
dt.Rows.Add(New String() { "Three", "Home" })
dt.Rows.Add(New String() { "Other", "Root" })
 
'Add DataTable to the DataSet
ds.Tables.Add(dt)
'Add a relationship
ds.Relations.Add("TreeParentChild", ds.Tables("AddressLists").Columns("Name"), ds.Tables("AddressLists").Columns("Parent"), False)
 
''' <summary>
''' To Populate TreeView
''' </summary>
Public Sub PopulateTree(ByVal dr As DataRow, ByVal pNode As TreeNodeAdv)
  'To iterate through all the rows in the DataSet
  For Each row As DataRow In dr.GetChildRows("TreeParentChild")
    'Creating a TreeNode for each row
    Dim cChild As New TreeNodeAdv(row("Name").ToString())
    'Add cChild node to the pNode
    pNode.Nodes.Add(cChild)
    'Recursively build the tree
    PopulateTree(row, cChild)
  Next row
End Sub
 
''' <summary>
''' Button Click event
''' </summary>
Private Sub buttonAdv1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonAdv1.Click
  'Iterate all the rows in the DataSet
  For Each dr As DataRow In ds.Tables("AddressLists").Rows
    If dr("Parent").ToString() = "0" Then
      'Creates a TreeNode if the parent equals 0
      Dim root As New TreeNodeAdv(dr("Name").ToString())
      treeViewAdv1.Nodes.Add(root)
      'Recursively builds the tree
      PopulateTree(dr, root)
    End If
  Next dr
  'Expands all the tree nodes
  treeViewAdv1.ExpandAll()
End Sub

Screenshot

Load the data from datatable

Samples:

C#: TreeView DataBinding - C#

VB: TreeView DataBinding - VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon