0

I have som problem populating a treeview in c# from database. The table looks like this:

| code    | description | attached to | 
---------------------------------------
| P001    | TEST001     | NULL        |
| P0001   | TEST002     | P001        |
| P002    | TEST003     | NULL        |
| P00201  | TESTXXX     | P002        |
| P00222  | TESTXXX     | P002        |
| P002020 | TESTSSS     | P00222      |

This does not work.

protected void PopulateTreeView(TreeNodeCollection parentNode, string parentID, DataTable folders)
    {

        foreach (DataRow folder in folders.Rows)
        {
            // if (Convert.ToInt32(folder["Attached to"]) == parentID)
            if (string.IsNullOrEmpty(folder["Attached to"].ToString()))
            {

                String key = folder["code"].ToString();
                String text = folder["description"].ToString();
                TreeNodeCollection newParentNode = parentNode.Add(key, text).Nodes;
                //PopulateTreeView(newParentNode, Convert.ToInt32(folder["code"]), folders);
                PopulateTreeView(newParentNode, folder["code"].ToString(), folders);
            }
        }
    }
3
  • 1
    Why does it not work? Error, invalid node, something else? Please explain what the problem is. Commented May 28, 2014 at 12:16
  • program stucks in infinite loop Commented May 28, 2014 at 12:17
  • Recursion method always starts from the first row and then call itself in loop. Well, in your example it stuck on second row. Commented May 28, 2014 at 12:21

1 Answer 1

1

The problem is with your If condition. You should search for rows that are attached to parentID. Here is corrected method with test:

    public void Test_PopulateTreeViews()
    {
        var rootNode = new TreeNode();
        var folders = new DataTable();

        folders.Columns.Add("code");
        folders.Columns.Add("description");
        folders.Columns.Add("Attached to");
        folders.Rows.Add("P001", "TEST001", string.Empty);
        folders.Rows.Add("P0001", "TEST002", "P001");
        folders.Rows.Add("P002", "TEST003", null);
        folders.Rows.Add("P00201", "TEST003", "P002");
        folders.Rows.Add("P00222", "TESTXXX", "P002");
        folders.Rows.Add("P002020", "TESTSSS", "P00222");

        PopulateTreeView(rootNode, string.Empty, folders);
    }

    private void PopulateTreeView(TreeNode parentNode, string parentID, DataTable folders)
    {
        foreach (DataRow folder in folders.Rows)
        {
            if (folder["Attached to"].ToString() == parentID)
            {
                String key = folder["code"].ToString();
                String text = folder["description"].ToString();

                var newParentNode = new TreeNode(key, text);
                parentNode.ChildNodes.Add(newParentNode);
                PopulateTreeView(newParentNode, folder["code"].ToString(), folders);
            }
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

sorry but this does not work. infinite loop. Maybe I was unclear. The table does not have null values in attached to. The attached field is blank when there is no parent
I corrected the code so it could be compiled and tested. But main idea remains. As far as I saw resutls it creates tree correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.