Click here to Skip to main content
15,868,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to plot a tree using treeview in asp.net.
issue facing is i am able to compare parent node but able to compare parent1 node, it is resulting in duplicate parent1 nodes.
example:
if data is as follows
MainNode1.Parent1.Child1, MainNode1.Parent1.Child2 and so on....
I get multiple Parent 1 under MainNode1.
Kindly help me out
TIA

What I have tried:

<pre>NDA.Fill(dt); //tag data in data table 
int max_row = Convert.ToInt32(dt.Rows.Count);
                if (max_row > 0) //if no tags dont execute
                {
                    try
                    {
                        for (int i = 0; i <= max_row; i++)
                        {

                            string combination = Convert.ToString(dt.Rows[i["tag_name"]);
                            string[] data = combination.Split('.');
                            TreeNodeCollection nodes = TreeView1.Nodes;
                            TreeNode dnode = new TreeNode();
                            //int pnode = 0;
                            //TreeNode FindNode = null;
                            foreach (string name in data)
                            {
                                node = TreeView1.FindNode(name);
                                if (node == null)
                                {
                                    node = new TreeNode(name);
                                    nodes.Add(node);
                                }
                                nodes = node.ChildNodes;
                          }

                        }

                    }
                    catch (Exception e1)
                    {
                    }
Posted
Updated 14-Oct-19 8:33am

1 solution

Try this:
C#
NDA.Fill(dt);

foreach (DataRow row in dt.Rows)
{
    string tagName = Convert.ToString(row["tag_name"]);
    string[] parts = tagName.Split('.');
    TreeNodeCollection nodes = TreeView1.Nodes;
    for (int i = 0; i < parts.Length; i++)
    {
        string valuePath = string.Join(TreeView1.PathSeparator, parts, 0, i);
        TreeNode node = TreeView1.FindNode(valuePath);
        if (node == null)
        {
            node = new TreeNode(parts[i]);
            nodes.Add(node);
        }
        
        nodes = node.ChildNodes;
    }
}
The FindNode method[^] expects the full path of the node to find, whereas you're just passing the value of the current node.

So, for example, instead of looking for a node with the path MainNode1/Parent1, you're looking for a node with the path Parent1. Since you never add Parent1 to the root of the tree, that node never exists.
The value path contains a delimiter-separated list of node values that form a path from the root node to the current node.
 
Share this answer
 
Comments
Member 12694392 14-Oct-19 23:13pm    
cool it worked with following changes on Richard post

string valuePath = string.Join(TreeView1.PathSeparator.ToString(), parts, 0, i+1);

Thanks Richard Deeming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900