Re: populate treeview from single table parent-child relationship
From: Robert Samuel White (newsgroups.noreply_at_enetwizard.net)
Date: 05/04/04
- Next message: William Vaughn: "Re: MDAC 2.8 Issues"
- Previous message: tk042383: "Inserting C# datetime milliseconds into sql Server DateTime"
- In reply to: Frans Bouma [C# MVP]: "Re: populate treeview from single table parent-child relationship"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 3 May 2004 20:21:00 -0400
Thanks, Frans, I was surprised to see my post in here as I did not think it
got posted (I waited a whole day before posting again) and now I've actually
got something workable. I will definitely check out your code so I can get
a real grasp on this stuff. I really appreciate you taking the time to post
this for me!
"Frans Bouma [C# MVP]" <perseus.usenetNOSPAM@xs4all.nl> wrote in message
news:xn0dhtwva4hsjl001@msnews.microsoft.com...
> Robert Samuel White via .NET 247 wrote:
>
> > Hi there! I've been looking around with little luck on how to populate
a
> > treeview from a single table parent-child relationship. It is simply a
> > table of users, where each are identified with a unique UserId and have
a
> > corresponding ParentId (pointing to their boss' UserId). If it's a
> > top-level user, then the ParentId is set to 0. Can someone please help
me
> > figure out the best way to populate this list? I'm unsure of how to do
the
> > logic behind the algorithm. Any help would be greatly appreciated.
>
> It's pretty simple: (I assume your data is in a datatable)
>
> TreeNode rootNode = null;
> Hashtable idToNode = new Hashtable(users.Rows.Count);
>
> // first walk the list of users and create Treeview Nodes.
> // we need 2 loops, as we can encounter a user with a parent
> // and the parent isn't processed yet.
> for(int i=0;i<users.Rows.Count;i++)
> {
> TreeNode newNode = new TreeNode(users.Rows[i]["UserId"].ToString());
> idToNode.Add(users.Rows[i]["UserId"], newNode);
> if((int)users.Rows[i]["ParentId"]==0)
> {
> // found the root
> rootNode = newNode;
> }
> }
>
> // now we're going to build the tree!
> for(int i=0;i<users.Rows.Count;i++)
> {
> TreeNode nodeToAdd = (TreeNode)idToNode[users.Rows[i]["UserId"]];
> // find parent
> if((int)users.Rows[i]["ParentId"]!=0)
> {
> TreeNode parentNode = (TreeNode)idToNode[users.Rows[i]["ParentId"]];
> parentNode.Nodes.Add(nodeToAdd);
> }
> }
>
> // add the complete node structure to the treeview control
> myTreeView.Nodes.Add(rootNode);
>
> voila, an O(2) algo. This is from my bare head, so I might have made a
syntax
> error here and there, but you get the idea.
>
> FB
>
> --
> Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
> My .NET Blog: http://weblogs.asp.net/fbouma
> Microsoft C# MVP
- Next message: William Vaughn: "Re: MDAC 2.8 Issues"
- Previous message: tk042383: "Inserting C# datetime milliseconds into sql Server DateTime"
- In reply to: Frans Bouma [C# MVP]: "Re: populate treeview from single table parent-child relationship"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|