Re: populate treeview from single table parent-child relationship

From: Robert Samuel White (newsgroups.noreply_at_enetwizard.net)
Date: 05/04/04


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



Relevant Pages

  • Getting WWW::Mechanize to submit a form
    ... I'm trying to use this module to populate and submit a form on a remote server, but *apparently* it is not doing either. ... I've tested a similar script myself, taking form data from a static HTML form, and posting it to another static HTML form on a different server, and it works fine. ... "Others make web sites. ...
    (comp.lang.perl.modules)
  • Re: Forwarding Contacts with Note Field Populated
    ... > How do I get this field to populate? ... If you forward as an Outlook contact, then it will carry over the notes ... This posting represents my own opinion, ... Please do not send e-mail directly to this alias. ...
    (microsoft.public.outlook)
  • MS ADODC
    ... I apologise for posting this into vb.net but i can find a vb6 newsgroup ?? ... I am using ADODC control to populate a screen in an application which is multi-user via terminal server. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Insert with parameters
    ... The section "To populate a new table with data" in: ... has an example of using parameters in queries. ... This posting is provided "AS IS" with no warranties, ... > Mats Boberg. ...
    (microsoft.public.sqlserver.ce)
  • RE: codepage in BizTalk 2004
    ... Frans, can you outline the scenario where you have to do this? ... - Allen Zhang (MSFT) ... This posting is provided "AS IS" with no warranties, ...
    (microsoft.public.biztalk.general)