0
votes

I have a big problem. I want to populate a treeview by some data i got.

There is a root node. To this rootnode i want to add some other nodes, defined by my data. The data includes a name, an ID and a ParentID

The data is stored in an OLEDB but i already have stored in my programm too.

Let me give you an example because my english isn't the best:

Data:

Testdata 1 0

Testdata1 2 0

Testdata3 3 1

Testdata4 4 2

Testdata5 5 3

Testdata6 6 4

Testdata7 7 4

So my tree should look similar to that:

+Root

++Testdata1

++++Testdata3

++++++Testdata5

++Testdata2

++++Testdata4

++++++Testdata6

++++++Testdata7

Have you any idea how i can achieve this?

regards

Schlinger

1

1 Answers

0
votes

You will need a recursive method to walk the tree.

private void AddNode(List<Data> data, int parent)
{
    var parent = data.FirstOrDefault(x => x.ID == parent);
    var dataItems = data.Where(x => x.Parent == parent);
    foreach(var dataItem in dataItems)
    {
        AddNode(data, dataItem.ID);
    }
    Tree.Nodes.Add(parent.Name);
}