Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
actually i have one treeview.which have 3 levels of node.as like follwing tree structure

Dept
|
--- SubDept
|
------KPI



there are 3 textbox
1.txtDept
2.txtsubdept
3.txtkpi

there are 3 condition:
1.if user click on mainroot node(Dept)
when user click on 1st node i.e dept(Root node),dept name shown into txtdept,and 1st corresponding subdeptname display into txtsubdept and corresponding 1st KPI display into txtkpi

2.if user click on root node(SubDept)
when user click on next Root node i.e subdept,subdept name display into txtsubdept and corresponding 1st KPI display into txtkpi and parent node(Dept name) shown into txtdept.


3.if user click on child node of subdept(KPI)
when user click child node of subdept(KPI),that kpinode name display into txtkpi,and its corresponding parent i.e(SubDept)display into txtsubdept,and parent of parent node name (Dept) display into txtdept


how to handled this three condition?and which event fire for this?

mycode:

private void treeViewKPI_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode node;
node = treeViewKPI.SelectedNode;
if (node.TreeView.Nodes.Contains(node))
{
string nodeName;
panelDisplayKPI.Visible = true;
txtDept.Text = node.Text.ToString();
nodeName = node.FirstNode.Text.ToString();
textSubDept.Text = node.FirstNode.Text.ToString();
if (node.FirstNode.Nodes.Count > 0)
{
txtDisplayKPI.Text = node.FirstNode.Nodes[0].Text;
}
else
{
txtDisplayKPI.Text = "";
}
}
else
{

txtDept.Text = node.Parent.Text.ToString();
textSubDept.Text = node.Text.ToString();
if (node.FirstNode.Nodes.Count > 0 || node.FirstNode!=null)
{
txtDisplayKPI.Text = node.FirstNode.Nodes[0].Text;
}
else
{
}
}
btnSave.Enabled = true;
btnDelete.Enabled = true;
}
Posted

1 solution

You can use the 'Level Property of the TreeNode object in the Microsoft TreeView Control to simplify this:
C#
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeNode currentNode = e.Node;

    switch (currentNode.Level)
    {
        case 0:
            textBox1.Text = currentNode.Text; // dept
            textBox2.Text = currentNode.Nodes[0].Text; // sub dept
            textBox3.Text = currentNode.Nodes[0].Nodes[0].Text;  // KPI
            break;
        case 1:
            textBox1.Text = currentNode.Parent.Text;
            textBox2.Text = currentNode.Text;
            textBox3.Text = currentNode.Nodes[0].Text;
            break;
        case 2:
            textBox1.Text = currentNode.Parent.Parent.Text;
            textBox2.Text = currentNode.Parent.Text;
            textBox3.Text = currentNode.Text;
            break;
    }
}
 
Share this answer
 
Comments
Gauri Bharde 1-Jan-14 4:12am    
thanks sir i will try it immediately

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