Click here to Skip to main content
15,904,500 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi,

I am using a treeview,in which i want to expand that node only which is selected,and others are collapse.I have used a code for doing this,but i want to know that how to bind the tree on pageload by which the code is run.


my code is given below:
C#
protected void Tree_SelectNodeChange(object sender, EventArgs e)
{
   var tree = (TreeView)sender;
   foreach (TreeNode node in tree.Nodes)
   {
       node.CollapseAll();
   }
   ExpandToRoot(tree.SelectedNode);
}

private void ExpandToRoot(TreeNode node)
{
   node.Expand();
   if (node.Parent != null)
   {
       ExpandToRoot(node.Parent);
   }
}



thanks
Posted
Updated 22-Mar-12 0:12am
v2
Comments
ProEnggSoft 22-Mar-12 6:12am    
Edit: Indentation adjusted, code tags added.
Pablo Aliskevicius 22-Mar-12 8:12am    
Any reason not to let the user choose what to expand and what to collapse? That's what most people, IMHO, expect.

1 solution

I am assuming that this is WinForm. Everything on WinForm is manual, which means that you will have to collapse every node. I would recommend that you keep a pointer to the most recently expanded node, and on the node selected event, to to that node and collapse the node and its parents.
 
Share this answer
 

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