TreeView displays the child nodes of the root node but does not display child nodes of child nodes

I am making a TreeView using ASP.NET web forms. The problem I am facing is that the child nodes of the root node(s) are being displayed, but the child nodes of child nodes are not being displayed on the web page.
To explain this in a better way, I have attached a screenshot below that shows the data in the database.

TreeView displays the child nodes of the root node but does not display child nodes of child nodes

In this table, node1 and node2 are root nodes, it means they have no parent so their parent id is 0. Moving on to the childnode1,childnode2,childnode3. They are being displayed fine but the childnode4 and childnode5 (which are children of childnode2 and childnode3) do not display.

Here is the code I’ve written

protected void Page_Load(object sender, EventArgs e)
    {
        var db = new treeContext();
        var treeData = db.trees.ToList();
        
        foreach (var item in treeData)
        {
            if (item.ParentId==0)
            {
                TreeNode node = new TreeNode(item.Name.ToString());
                TreeView1.Nodes.Add(node);
            }
            else
            {
                    var getParent = db.trees.Where(o => o.Id == item.ParentId).FirstOrDefault()?.Name;
                    TreeNode parentNode = TreeView1.FindNode(getParent);
                    if (parentNode != null)
                    {
                        TreeNode childnode = new TreeNode(item.Name.ToString());
                        parentNode.ChildNodes.Add(childnode);
                    }
                }
            }
        }

The output trying to get
TreeView displays the child nodes of the root node but does not display child nodes of child nodes

TreeView displays the child nodes of the root node but does not display child nodes of child nodes
The output I’m getting

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Quite possibly related to this (it’s VB.NET but you get the idea) – Treeview.FindNode Not working for me on 2 levels deep

I think your “FindNode” is failing on anything other than top-level nodes.

Because you have more than one level of nesting, I’d suggest abandoning your list-type population approach and use a recursive approach instead, something like: –

protected void Page_Load(object sender, EventArgs e)
{
    var db = new treeContext();
    var treeData = db.trees.Where(x => x.ParentId == 0).ToList();

    LoadChildNodes(null, treeData, db);
}

private void LoadChildNodes(TreeNode parent, IList<TreeDatabaseItem> treeData, treeContext db)
{
    if (treeData?.Count != 0)
    {
        foreach (var item in treeData)
        {
            TreeNode node = new TreeNode(item.Name.ToString());

            if (parent == null)
            {
                TreeView1.Nodes.Add(node);
            }
            else
            {
                parent.Nodes.Add(node);
            }

            var childData = db.trees.Where(x => x.ParentId == item.ID).ToList();
            LoadChildNodes(node, childData, db);
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x