Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:32:00+00:00 2026-05-15T06:32:00+00:00

I am trying to create a TreeView nested structure with the use of self

  • 0

I am trying to create a TreeView nested structure with the use of self referencing table fields. Here is a simple example:

Category 1 
      Product 1 
          Toy 1 
          Toy 2 
      Product 2 
          Toy 3 
          Toy 4 

more categories..

The database table has a single table called “Category”. The ParentCategoryId points to the Category which is the parent. So, for the Category 1 the ParentCategoryId is null since it is parent. For Product 1 the ParentCategoryId is that of the Category 1 id and for Toy 1 the ParentCategoryId is that for the Product 1 id.

I am using the following code but it does not generate the TreeView (ASP.NET) successfully.

 public void BuildTree(List<Category> categories, TreeNode treeNode)
    {
        if (treeNode == null) return;

        TreeNode tnAdd = null;
        var categoryId = Guid.NewGuid();

        foreach (var category in categories)
        {
            if (category.IsBaseCategory)
            {
                tnAdd = new TreeNode();
                tnAdd.Text = category.Description;

                BuildTree((from c in categories
                           where c.ParentCategoryId == category.CategoryId
                           select c).ToList<Category>(), tnAdd);
            }
            else
            {
                tnAdd = new TreeNode();
                tnAdd.Text = category.Description;

                BuildTree((from c in categories
                           where c.ParentCategoryId == category.CategoryId
                           select c).ToList<Category>(), tnAdd);
            }

            if (tnAdd != null)
                treeNode.ChildNodes.Add(tnAdd);              
        }
    }

Does this require recursion!

and here is the result I get:

  80W 
  40W 
  40W 
  Light Bulbs 

   Flourecent 
   Incedecent 

  60W 
  80W 
  60W 
  Flourecent 

   40W 
   80W 
   60W 

  Incedecent 

   80W 
   40W 
   60W 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:32:01+00:00Added an answer on May 15, 2026 at 6:32 am

    What isn’t successful?
    If its because you see nothing … I don’t see where you’re ever adding the root node to the actual tree control. tnAdd needs to be added to the tree control somewhere.

    If it’s because your not getting everything you expect: Unless you already have recursion going on somewhere and don’t realize it, I can’t see how the above code is ever going to get to the toy level. You say “base”, then “child” in the above code which covers two levels. You have three levels in your sample data, so at some point you need to account for adding the toys. You can write it recursively if you need to have n levels. If you only have three levels, you can just repeat yourself.

    —– UPDATES FOR OP UPDATES

    Looking at your code, what you have is this:

       for each category {
        if it is base
           add its children
        else if it is not base
           add its children
    
        add it to the tree
       }
    

    This means every item is hit in the first foreach and added to the tree, rather than per level.
    what you want is

    for each category{ 
        if it is base
           add base's children
    
           for each child [
              add child's children
              add child to the tree
           ]
    
           add base the tree
    }
    

    Something closer to this (I don’t have time to test right now, sorry) should come close to working

     public BuildTreeTop(List<Category> categories, TreeNode treeNode)
     {
        BuildTree((from c in categories
                               where c.IsBaseCategory == true
                               select c).ToList<Category>(), categories, tnAdd);
     }
    
     public void BuildTree(List<Category> currentLevel, List<Category> allCategories, TreeNode treeNode)
        {
            if (treeNode == null) return;
    
            TreeNode tnAdd = null;
            var categoryId = Guid.NewGuid();
    
            foreach (var category in currentLevel)
            {
                tnAdd = new TreeNode();
                tnAdd.Text = category.Description;
    
                BuildTree((from c in allCategories
                            where c.ParentCategoryId == category.CategoryId
                            select c).ToList<Category>(), allCategories, tnAdd);
    
    
                if (tnAdd != null)
                    treeNode.ChildNodes.Add(tnAdd);              
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.