Display a list of nodes in a TreeView
I have a node object that indicates the node level
public class Node
{
public int Level { get; set; }
}
I want to use an ordered list of nodes to build a treeview
var nodes = new[]
{
new Node(){Level = 0},
new Node(){Level = 1},
new Node(){Level = 1},
new Node(){Level = 1},
new Node(){Level = 2},
new Node(){Level = 1},
new Node(){Level = 2},
new Node(){Level = 2},
new Node(){Level = 3},
};
What is the most efficient way to do this.
thanks
Rohan
source to share
Following on from Boo, you will want to keep the node stack as you go up. If the level is higher in the current node, push the previous node onto the stack and use it as parent. If the level is lower than the previous node, pop n nodes from the stack (n = previous_level - current_level) and use the new top of the stack as parent.
source to share
Scroll through the list of nodes. Starting at index 0, write down that node Level. In all subsequent indices, check the level in relation to the last recorded level. If higher, add it as a subnode of the last added node. If below, well ... if problems with lower cases .. find the appropriate node to add it to.
source to share