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

I am adding nodes for treeview in a method by getting each node from a calling method. But while in loop i get a node whose text is "Other".

I want to add this node as well to treeview but it should display as last descendant.

How to i achieve this? Any help
Posted
Comments
BillWoodruff 26-Oct-14 6:34am    
Windows Forms ? WPF ? ASP.NET ?

The solution will depend on how you answer this question: the "last descendant" of what ? Do you have a reference to the node you want to make the "Other" node the last child of ?

Does your tree have a single root node, or more than one root-level node ?
DamithSL 26-Oct-14 6:35am    
update question with your code
yash35 26-Oct-14 7:25am    
m_oTree.Nodes.Find(oNode.ParentKey, true)[0].Nodes.Add(oNode.Key, oNode.DisplayText, ReturnImageListIndex(oNode), ReturnImageListIndex(oNode));

i am adding nodes to treeview with above line of code.
it is windows forms. I want it to be last descendant of its parent node i.e root node.
My tree has a single root node

1 solution

Yash35 wrote:
m_oTree.Nodes.Find(oNode.ParentKey, true)[0].Nodes.Add(oNode.Key, oNode.DisplayText, ReturnImageListIndex(oNode), ReturnImageListIndex(oNode));
 
i am adding nodes to treeview with above line of code.
it is windows forms. I want it to be last descendant of its parent node i.e root node.
My tree has a single root node
In the WinForm Microsoft TreeView there is no .ParentKey property of a TreeNode, and someNode.Parent.Key would be an error, also.

While it is possible you have sub-classed TreeNode, and added a 'ParentKey field ... I doubt that.

If you have a single root node in your design, then why not keep a pointer to the Nodes Collection of the root node; then, you can be certain that if you use 'Add on that collection, the TreeNode you add will be the last TreeNode in the TreeView
C#
// create a variable in Form scope
private TreeNodeCollection rootNodes;

// in a method in your application code:
// at a place where the root-node has been created,
// for example: in the Form Load EventHandler
private void YourForm_Load(object sender, EventArgs e)
{
    rootNodes = yourTreeView.Nodes;
    
    // whatever else ...
}

// example of using 'rootNodes:
// in some method:
private void someAddTreeNodeMethod(string nodeText)
{
   rootNodes.Add(new TreeNode(nodeText));
   // do the right thing to set its ImageList pointer, etc.
}
 
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