how to load a 4 level tree view in

Posted on 2009-02-23
Last Modified: 2012-05-06
I have table called Topic whose structure i am sending with this question.
this topic has columns ParentPKey and Pkey.

and I want to load tree some thing like i have given in the code section

now I want to add all those records in to the tree view who has parentkey =0 as the parent node. then I want to add those nodes whose parentpkey = pkey ( of those nodes whose parentkey = 0).

then I want to those nodes whose parentpkey is equal to the nodes which are added in the second step.
and so on so fourth.
my problem here is there might be a chance  that some where in the middle or in the end of records some node come whose parent is on the top of the tree.

right now i am using loops but it totally messed up.
any one could help me here.





Open in new window

Question by:ziorrinfotech
    LVL 52

    Accepted Solution

    You could do something along the lines of the following. At the end of this code the "rootNodes" List will contain all of the top-level nodes (i.e. nodes with no parent) with all of their children nested underneath.

    Dictionary<int, TreeNode> nodes = new Dictionary<int, TreeNode>();
    List<TreeNode> rootNodes = new List<TreeNode>();
    // i'll assume a data reader as the datasource
    while (reader.Read())
        TreeNode newNode = new TreeNode(reader.GetString("TopicName");
        int parentId = reader.GetInt32("ParentPKey");
        if (nodes.ContainsKey(parentId))
            parent = nodes[parentId];
        if (parentId == 0)

    Open in new window


    Expert Comment

    This is part of the solution I am trying to write
    public void PopulateTreeView()
                Dictionary<int, TreeNode> nodes = new Dictionary<int, TreeNode>();
                List<TreeNode> rootNodes = new List<TreeNode>();
                SampleDB db = new SampleDB();
                var query = from o in db.OptionsMenu
                            orderby o.MenuID, o.ParentMenuID ascending
                            select o;
                int i = query.Count();
                if (i > 0)
                    foreach(var q in query)
                        TreeNode newNode = new TreeNode(q.MenuName);
                        int parentID = Int32.Parse(q.ParentMenuID.ToString());

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    Title # Comments Views Activity mvc - Time 37 45 mvc 2 30
    call function 8 34
    401 - Error 6 11
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now