سؤال

I'm working on a little program, which grabs some data and should display them on a Treeview or DotnetBars advTree control.

On a button click event there will be collected some data and the (string)output looks like this. (structure of treeviews)

myTree.add(1,-1,'','');
myTree.add(260, 1, 'All Releases', '');
myTree.add(6,260, 'Cat1', ''
myTree.add(7,6, '20.0.2'', ''
myTree.add(8,7, '20.0.21', '&some stuff'
myTree.add(9,7, '20.0.20a', '&some stuff'
myTree.add(10,7, '20.0.5', '&some stuff'
myTree.add(11,260, 'Cat2', ''
myTree.add(12,11, '12.2', ''
myTree.add(13,12, '12.2.77', '&some stuff'
myTree.add(14,12, '12.2.26', '&some stuff'

First Column contains the Node-ID and the second column contains the ID, which it is a child-node from. The third contains the Node Text, which should be shown in the treeview, the last is for later purpose (value output).

Result: (Treeview example)

  • All Releases
    • Cat1
      • 20.0.2
        • 20.0.21
        • 20.0.20a
        • 20.0.5
    • Cat2
      • 12.2
        • 12.2.77
        • 12.2.26

How can I get the Treeview items added programmatically with all its child-items?

هل كانت مفيدة؟

المحلول

class MyTreeNode
{
    public String NodeTitle;
    public List<MyTreeNode> children;
}

I would build my desired structure into an object then recursively search through the children property to see if I needed to add more branches.

public AddBranch(MyTreeNode parentNode)
{
    MyTree.Add(parentNode.NodeTitle);

    foreach (var node in parentNode.children)
    {
        AddBranch(node);
    }
}

// Note this is a rough mock code just to give you an idea of what recursion is.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top