I have a treeview with companies. Each company can have subsidiary companies. Each subsidiary company can have its own subsidiary companies and this can continue indefinitely. Each company has its own revenues, and still has fullEarnings which is equal to the sum of all the revenues of the subsidiaries of this company.

I need to calculate the profit for each company, if you know each company's own earnings.

Example:

Counting example

I have a ParentID column in the database whose values ​​refer to the parent company ID.

How can i do this? Maybe through recursion?

    1 answer 1

    Well, for example, like this.

    private List<TreeViewNode> _GetFullBranch(TreeViewNode node) { List<TreeViewNode> list = new List<TreeViewNode> { node }; foreach (var child in node.Items) list.AddRange(_GetFullBranch(child as TreeViewNode)); return list; }