)
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: February 13, 2015 |
Last Revised Date: February 5, 2020

Populate TreeViewAdv from DataTable

Currently in TreeViewAdv, there is no direct support for loading TreeNodeAdv from DataTable. You need it to populate TreeViewAdv, by recursively loading data from the DataTable. The following code example is to demonstrate the same.

C#

//Create Data Table
dt = new DataTable("CaseNotes");
dt.Columns.Add("NoteID", typeof(string));
dt.Columns.Add("NoteName", typeof(string));
//Add sample data.
dt.Rows.Add(new string[] { "1", "One" });
dt.Rows.Add(new string[] { "2", "Two" });
//Create nodes
private void CreateNodes()
{
    //Create TreeNodesAdv
    DataRow[] rows = new DataRow[dt.Rows.Count];
    dt.Rows.CopyTo(rows, 0);
    //TreeView node creation
    treeViewAdv1.BeginUpdate();
    treeViewAdv1.Nodes.Clear();            
    TreeNodeAdv[] nodes = RecurseRows(rows);
    treeViewAdv1.Nodes.AddRange(nodes);
    // Notify the TreeView to resume painting.
    treeViewAdv1.EndUpdate();
}
//ReCurseRows function
private TreeNodeAdv[] RecurseRows(DataRow[] rows)
{
     List<TreeNodeAdv> nodeList = new List<TreeNodeAdv>();
     TreeNodeAdv node = null;
     foreach (DataRow dr in rows)
     {
        node = new TreeNodeAdv(dr["NoteName"].ToString());
        noteID = Convert.ToInt32(dr["NoteID"]);
        node.Text = noteID.ToString();
        if (nodeList.Find(FindNode) == null)
           {
               DataRow[] childRows = dt.Select("ParentNoteID = " + dr["NoteID"]);
               if (childRows.Length > 0)
                  {
            //Recursively call this function for all childRowsl
                     TreeNodeAdv[] childNodes = RecurseRows(childRows);
                     // Add all child nodes to this node.
                     node.Nodes.AddRange(childNodes);
                  }
                nodeList.Add(node);
            }
      }
            //Convert this List<TreeNodeAdv> to an array so it can be added to the parent node/TreeView
            TreeNodeAdv[] nodeArr = nodeList.ToArray();
            return nodeArr;
}

VB

'Create Data Table
dt = New DataTable("CaseNotes")
dt.Columns.Add("NoteID", GetType(System.String))
dt.Columns.Add("NoteName", GetType(System.String))
'Add sample data.
dt.Rows.Add(New String() {"1", "One"})
dt.Rows.Add(New String() {"2", "Two"})
'Create nodes
Private Sub CreateNodes()
    'Create TreeNodesAdv
    Dim rows() As DataRow = New DataRow((dt.Rows.Count) - 1) {}
    dt.Rows.CopyTo(rows, 0)
    'TreeView node creation
    treeViewAdv1.BeginUpdate()
    treeViewAdv1.Nodes.Clear()
    Dim nodes() As TreeNodeAdv = RecurseRows(rows)
    treeViewAdv1.Nodes.AddRange(nodes)
    ' Notify the TreeView to resume painting.
    treeViewAdv1.EndUpdate()
End Sub
'ReCurseRows function
Private Function RecurseRows(ByVal rows() As DataRow) As TreeNodeAdv()
     Dim nodeList As List(Of TreeNodeAdv) = New List(Of TreeNodeAdv)
     Dim node As TreeNodeAdv = Nothing
     For Each dr As DataRow In rows
         node = New TreeNodeAdv(dr("NoteName").ToString)
         noteID = Convert.ToInt32(dr("NoteID"))
         node.Text = noteID.ToString
         If (nodeList.Find(FindNode) Is Nothing) Then
             Dim childRows() As DataRow = dt.Select(("ParentNoteID = " + dr("NoteID")))
             If (childRows.Length > 0) Then
                'Recursively call this function for all childRowsl
                Dim childNodes() As TreeNodeAdv = RecurseRows(childRows)
                'Add all child nodes to this node.
                 node.Nodes.AddRange(childNodes)
              End If
              nodeList.Add(node)
         End If
      Next
      'Convert this List<TreeNodeAdv> to an array so it can be added to the parent node/TreeView
      Dim nodeArr() As TreeNodeAdv = nodeList.ToArray
      Return nodeArr
End Function

Sample: http://www.syncfusion.com/downloads/support/directtrac/general/TreeViewFromDataTable1602064888.zip

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