Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I am trying to display a tree view in below format and data will be populated from external source.Could you please share your ideas how we can bind the data to nodes while initialing the form itself.

+---Name1
      |
      +------Name1
      |         |
      |         +-----Name1
      |         |        |
      |         |        +-----Name1
      |         |        |
      |         |        +-----Name2
      |	        |        |
      |	        |        +-----Name3
      |         |                |
      |         |                +-----Name1
      |	        |                |
      |	        |                +-----Name2
      |         |
      |         +-----Name2
      |	        |
      |	        +-----Name3
      |
      +------Name2
      |         |
      |         +-----Name1
      |         |
      |         +-----Name2
      |	        |
      |	        +-----Name3
      |    
      +------Name3
      |         |
      |         +-----Name1
      |         |
      |         +-----Name2
      |	        |
      |	        +-----Name3
      |
      +------Name4
                |
                +-----Name1
                |
                +-----Name2
      	        |
      	        +-----Name3

Code :
public List ScriptCmnd(string s)
        {

            si.StartInfo.Arguments = s;
            si.Start();
            string output = si.StandardOutput.ReadToEnd();
            List result = output.Split('\n').ToList();
            si.Close();
            return result;
        }

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if ( e.Node.Level != 0)
            {
                string removeStr = "Project.pj";
                List result = ScriptCmnd("/c si viewproject --no --fields=name --project='" + e.Node.Name);
                result.RemoveAll(x => x == "" || !(x.Contains("project.pj")));
                if (result.Count != 0)
                {
                    foreach (string sr in result)
                    {
                        TreeNode trChild = new TreeNode();
                        trChild.Text = sr;
                        trChild.Name = e.Node.Name.Remove(e.Node.Name.IndexOf(removeStr, StringComparison.OrdinalIgnoreCase), removeStr.Length) + sr;
                        
                        treeView1.SelectedNode.Nodes.Add(trChild);
                        treeView1.SelectedNode.ExpandAll();
                    }
                }
            }
        }
Posted
Updated 18-Dec-12 2:10am
v2
Comments
[no name] 18-Dec-12 7:47am    
sandeep sangshetty 18-Dec-12 8:07am    
I tried to populate the internal nodes only when that upper node is selected but I want this to be happened before selecting the node itself.

Code as follows........

public List<string> ScriptCmnd(string s)
{

si.StartInfo.Arguments = s;
si.Start();
string output = si.StandardOutput.ReadToEnd();
List<string> result = output.Split('\n').ToList<string>();
si.Close();
return result;
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if ( e.Node.Level != 0)
{
string removeStr = "Project.pj";
List<string> result = ScriptCmnd("/c si viewproject --no --fields=name --project='" + e.Node.Name);
result.RemoveAll(x => x == "" || !(x.Contains("project.pj")));
if (result.Count != 0)
{
foreach (string sr in result)
{
TreeNode trChild = new TreeNode();
trChild.Text = sr;
trChild.Name = e.Node.Name.Remove(e.Node.Name.IndexOf(removeStr, StringComparison.OrdinalIgnoreCase), removeStr.Length) + sr;

treeView1.SelectedNode.Nodes.Add(trChild);
treeView1.SelectedNode.ExpandAll();
}
}
}
}

1 solution

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