One way could be to create a derived TreeNode so that it contains a List<data> :
// requires declaration of : using System.Windows.Forms; // sample data class public class data { public string Name; public int ID; } public class XTreeNode : TreeNode { List<data> theData = new List<data>(); public XTreeNode(string theNodeID) { this.Text = theNodeID; } public void addData(data newData) { theData.Add(newData); } }
Here's a (not elegant) example of what an instance of the above data structure (in WinForm) would look like: suppose you have a TreeView named "treeView1" in the form:
XTreeNode currentNode; data currentData; for (int i = 0; i < 10; i++) {
Regarding how you visually display the List<data> associated with each Node: the usual way would be to combine the Treeview with the ListView and synchronize their locations and element heights: then display the List<data> in the same βrowβ as the corresponding TreeNode
Of course, you can implement your own Node and NodeCollection objects that are completely independent of any control: this example is a mixed case of using a .NET control, which serves as both a data structure and a presentation mechanism.
A great example of the TreeView / ListView combination in CodeProject that has been maintained, updated, and expanded over the years: Phillip Piper: βMuch easier to use ListView,β first published in 2006, last updated in October 2009: its functionality is so rich that if compare profitably, imho with commercial components.
Billw source share