Articles in this section

How to populate WinForms TreeViewAdv from DataTable?

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: https://www.syncfusion.com/downloads/support/directtrac/general/TreeViewFromDataTable1602064888.zip


Conclusion

I hope you enjoyed learning about how to populate WinForms TreeViewAdv from DataTable.

You can refer to our WinForms TreeView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied