Adding father after the child to Treeview - c#

There is an existing TreeView with a single item. Lets say that tree consist from some object that has it own id, and parentId. Now i would like to add to tree parent item, that child reference to it by its parentId.
The idea is to handle input with a large amount of nodes that is not always sorted by father first.
How can i repopulate my tree with maximum efficiency ?

Take a look here : http://www.c-sharpcorner.com/uploadfile/9f4ff8/add-root-node-child-node-to-a-treeview-selected-node-at-runtime-and-rename-the-selected-node/
There is a method "Add Root node"
There is a method "Add child node"
I think you are ok.

Related

Winforms- design pattern to couple treenodes to datastructure

I currently have a data structure that displays file system tree's its basically an object with an array list of like typed objects. I want to display this in a treeview, and need to run some code when the tree view nodes are expanded, collapsed, selected, etc.
My question- is there a design pattern, or method of coupling between the data structure and the treeview that wont require me to search the whole DS tree for the selected node each time the user selects something?
Currently I am searching the data structure for a node with the same Text and Tag property as the selected node each time a relevant tree view event fires. I run into scenarios where if the node is not a leaf I have to go and re-search for its parent node, and I'm concerned about performance with large tree
Keep in mind, the data structure already inherits an object so I cant simply extend the treenode class.
Any help is greatly appreciated
You can still extend TreeNode but using composition to add an extra property that maps to the object that node represents.
You can also move the logic for the child nodes to this tree node rather than have it outside
public class DomainClass { /*...*/ }
public class DomainTreeNode: TreeNode
{
public DomainClass Element { get; private set; }
public DomainTreeNode(DomainClass element): base(element.Name)
{
Element = element;
/* iterate on element's children and add them to the node's
Childs collection ...*/
}
}

Empty expandable treenode in C#

I want to create an expandable empty treenode in C#, i.e. a treenode which is empty and has the [+] sign beside it. The reason is because initially it is empty, but once a node is clicked, I want to populate it with many child nodes.
The only problem I am facing is that empty treenodes aren't expandable, so I don't know what to do. Is there a way to solve this problem, or are there any workarounds?
You have to redraw the tree itself, or create an empty node and simply remove it when the parent node is expanded.
Personally, I'd go for option b). I've done this before, a while ago and thanks to the events raised by the TreeView it pretty easy to accomplish.
You can give the empty node a value like 'Loading...' so it gives some feedback to the user as well. :)
Add a dummy child node, and remove it when you expand.
Have a look at Josh Smiths excellent tutorial on treeviews. It allows lazy loading of child tree nodes by having a dummy node that is removed upon expansion.

How to implement a category hierarchy using collections

I have about 200 categories that are nested. I am currently reading the documention on the C5 generics library. I am not sure if the C5 library is overkill or not. I am looking at converting all my custom algorithms to the C5 implemention.
This is what I need. If a certain category is chosen i need to find its parents, siblings, direct children, and all children.
This is the way I have it set up. To find the:
Parents:
I start from the current location then loop through the list and find the current parent. When I find the parent I loop through the whole list again to find the next parent and so on.
Siblings:
I loop through the whole list and find all the nodes that have the same parent as the choosen node.
direct children:
I loop through the whole list and find all nodes that is a parent of the choosen node.
All Children:
This one took me a while to figure out. But I used recursion to find all children of the choosen node.
Is there a better way to implement something like this?
Why does it have to be collection-based? I would just create class Category with properties Category Parent and IList<Category> Children. That way, finding parent or predecessors is really easy (and fast!), finding children too, siblings are children of parent except the current category, so they are easy too. And finding descendants is trivial recursion.

ASP.NET: Display Leaf Node as Parent within TreeView Control

Is is possible to display a node without children as a leaf node within the .NET 3.5 TreeView control?
I have a treeview that displays a list of companies as nodes which would each display leaf nodes for documents belonging to that company.
If a company exists without any documents I would still like it to be rendered as parent (folder) but it renders as a leaf (file).
I could add a "No Documents" node to the company but this seems ugly and would add overhead to manage whether or not the node is there or should be added.
There doesnt seem to be any properties on TreeNode that would help here
Any ideas?
Thanks
The only way I discovered to resolve this issue is to add a "No Document" leaf to any empty parent folder

Best way to bind node collection to itemscontrol in Silverilght

I have a Silverlight project where the main objects are just a bunch of nodes that are joined to each other. A parent node can have many children.
I want to be able to bind the nodes to an itemscontrol or similar and so was wondering how to best structure the parent child relationship.
Is it OK to create a flat top level list of all nodes (List allNodes) and add each node to that, binding the list to the itemscontrol, then on top of that add each node to it's parent's 'childnodes' list to establish the structure - or am I doing some kind of ugly doubling up?
Just hoping there is some kind of best practice or pattern I can latch on to
Thanks
You should use the treeview found in the official silverlight toolkit. Here is an online demo.

Categories