Not tested, but the concept is: You need to remember the last chapter added to your treeview and the node used. If the chapter is still the same then add only the lesson using the chapter node, if the chapter is different add a new chapter node and set the variables used to guard your loop to the last values