Populating the TreeView Control

I have an N-Ary not sorted in any way and each node can have 0-N children. Given the data structure below, how can I populate the treeview, assuming you have an array of TermNodes and that array is the first level of the TreeView? I haven't been able to think of a recursive way to do this.

class TermNode
{
   public string Name;
   public string Definition;
   public List<TermNode> Children
}

      

+1


source to share


3 answers


Thank. All I got confused because I didn't understand that for a given TreeNode tn tn.Nodes.Add will return the added TreeNode Once you know the solution is straight forward so

private void /*TreeNode*/ RecursiveAdd(OntologyNode on, TreeNode tn)
{
    if (on.Children.Count == 0)
    {
        return;             
    }
    foreach (OntologyNode child in on.Children)
    {
        TreeNode tCur = tn.Nodes.Add(child.Name);
        tCur.Tag = child;//optional for some selected node events
        RecursiveAdd(child, tCur);               
    }
 }

      



and start recursive call

foreach( OntologyNode on in Nodes )
 {
     if (on.IsTopLevelNode == true)// internal not pertinent to this code snippet
     {
          TreeNode tn = tvOntoBrowser.Nodes.Add(on.Name);
          tn.Tag = on;
          if (on.Children.Count > 0)
          {
               RecursiveAdd(on, tn);
          }
     }

  }

      

0


source


Here's some code to get you started with recursion. It has not been tested (I cannot now), but you should understand:



public static void BuildTreeView(TreeNodeCollection Parent, List<TermNode> TermNodeList)
{
  foreach (TermNode n in TermNodeList)
  {
    TreeNode CurrentNode = Parent.Add(n.Name);
    // no need to recurse on empty list
    if (n.List.Count > 0) BuildTreeView(CurrentNode.Nodes, n.List);
  }
}

// initial call
List<TermNode> AllTermNodes = /* all your nodes at root level */;

BuildTreeView(treeView1.Nodes, AllTermNodes);

      

+1


source


Just pulled the Generics behind my back .. It worked beautifully. Worth seeing ...

public interface INode<T>
{
    List<T> Children { get; }
}
class TermNode:INode<TermNode>
{
   public string Name;
   public string Definition;
   public List<TermNode> Children { get; set; }
   public TermNode()
   {
       this.Children = new List<TermNode>();
   }
}

public class TreeBuilder<T> where T : INode<T>
{
    public Func<T, TreeNode> obCreateNodeFunc;

    public void AddNode(TreeView obTreeView, T obNodeToAdd, TreeNode obParentNodeIfAny) 
    {
        TreeNodeCollection obNodes;
        if (obParentNodeIfAny == null)
        {
            obNodes = obTreeView.Nodes;
        }
        else
        {
            obNodes = obParentNodeIfAny.Nodes;
        }
        int iNewNodeIndex = obNodes.Add(obCreateNodeFunc(obNodeToAdd));
        TreeNode obNewNode = obNodes[iNewNodeIndex];

        foreach (T child in obNodeToAdd.Children)
        {
            AddNode(obTreeView, child, obNewNode);
        }
    }
}

// calling code - Some class
    static TreeNode GetTreeNodeFor(TermNode t)
    {
        return new TreeNode(t.Name);  // or any logic that returns corr TreeNode for T
    }

    void Main()...
    {
       TermNode[] arrNodesList;    
       // populate list with nodes

       TreeBuilder<TermNode> tb = new TreeBuilder<TermNode>();
       tb.obCreateNodeFunc = GetTreeNodeFor;
       foreach (TermNode obNode in arrNodesList)
       {
           tb.AddNode(treeView, obNode, null);
       }
    }

      

+1


source







All Articles