stustarz Posted January 11, 2003 Posted January 11, 2003 Hello I currently have the following code to retrive records from a Dataset and populate the treeview. I would like to now place child nodes under each parent, from a different dataset where the seasons are equal (you'll see what i mean in the code). I'm really stuck as to how to do this: Current Code: 'Populate Treeview Dim varTVItem As TreeNode Dim varDSRow As DataRow tvSeasons.Nodes.Clear() dsSeasonData = DsSeasons1 For Each varDSRow In dsSeasonData.Tables("seasons").Rows varTVItem = New TreeNode() With varTVItem .Text = varDSRow.Item("Season") End With tvSeasons.Nodes.Add(varTVItem) Next Thanks in advance Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
*Gurus* divil Posted January 11, 2003 *Gurus* Posted January 11, 2003 Each Node is an object in itself, of type TreeNode. Every Node has a Nodes collection, of all its children. So to add a child node, use myNode.Nodes.Add(). Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
stustarz Posted January 12, 2003 Author Posted January 12, 2003 Hi Thanks for the reply The only problem i'm having is implementing this into the code. Would be able to tell me how to put this into the code I already have. Thanks Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
*Gurus* divil Posted January 12, 2003 *Gurus* Posted January 12, 2003 No, because I don't know how your data is structured. To add a child node to the one you're making, varTVItem.Nodes.Add("blah") Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted January 13, 2003 *Experts* Posted January 13, 2003 Each time you call Add, it returns the node that was added. Using that node, you can add children easily. As divil said, your DataSet may be structured to make things easier (or even recursive), but here's a sample to show how to add 3 parent nodes. Each parent node will have 2 children. // Add a parent node TreeNode node = treeView1.Nodes.Add("hello 1"); // Add two children. // This works because both children use "node" as the parent // to add to. The "node" variable is not reset // til farther down. node.Nodes.Add("child 1"); node.Nodes.Add("child 2"); // Here, node is reset to be the *next* // parent node. It's a parent because it's // added directly to the treeView1 control node = treeView1.Nodes.Add("hello 2"); node.Nodes.Add("child 1"); node.Nodes.Add("child 2"); node = treeView1.Nodes.Add("hello 3"); node.Nodes.Add("child 1"); node.Nodes.Add("child 2"); -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.