Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
D1 1____D4
1 1_____D5
1 1 1_____D7
1 1 1_____D8
1 1
1 1_____D6
1 1 1_____D9
1 1
1 1_____D7
1
D2
1____D12
1 1_____D10
1 1_____D11
1
1_____D15
1 1____D16
1 1____D17
D3
1_____D12
1_____D13


Hai to all..
this is the example of treeview now i want path
i.e out put shoud have to like this...

D1//D4//D5//D6//D7
D1//D4//D6//D9
D1//D4//D7
D2//D12//D10/D11
D2//D15//D16//D17
D3//D12//D13
This is required out put...

can anybody please help me....
Posted
Updated 1-Aug-18 17:50pm
v2
Comments
MarqW 24-Feb-11 3:34am    
Your output confuses me - D1//D4//D5//D6//D7, D7 is not a child of D6. Did you type your example out wrong?
ajitha.pusapati 24-Feb-11 4:47am    
up to D6 i got the path. but my requirement is D5 has chils D6 and D7. and D6 and D7 does have childs.
that why...
Sandeep Mewara 24-Feb-11 3:41am    
What effort have you made?
ajitha.pusapati 24-Feb-11 4:47am    
in c#.net windows forms
[no name] 24-Feb-11 3:44am    
can you be more specific and for this what code you have written so for and what error it shows?

I am assuming you're talking about WinForms TreeView?
In that case, try something like this;


C#
private string GetPath(TreeNode node)
{
    string path = "";
    for (TreeNode iterator = node; iterator != null; iterator = iterator.Parent)
    {
        path = String.Format("{0}//{1}", iterator.Name, path);
    }
    return path;
}
private void FindPaths(TreeNodeCollection nodes, 
                       IDictionary<TreeNode, string> paths)
{
    foreach (TreeNode node in nodes)
    {
        if (node.Nodes.Count == 0)
            paths[node] = GetPath(node);
        else
            FindPaths(node.Nodes, paths);
    }
}
private void some_click_event(object sender, TreeViewEventArgs e)
{
    IDictionary<TreeNode, string> paths = new Dictionary<TreeNode, string>();
    FindPaths(treeView.Nodes, paths);
}


The paths dictionary will contain all the paths with their leaf node as key.

Hope this helps,
Fredrik
 
Share this answer
 
In order to get "D1//D4//D5//D6//D7"
User this code : [TreeView name].PathSeparator = "//"
thats it'.
 
Share this answer
 
Comments
Dave Kreskowiak 2-Aug-18 0:01am    
You're only SEVEN YEARS too late and your answer doesn't solve the problem.

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