Wednesday, April 6, 2011

Hiding a node in a TreeView control

I had a web page with a treeView control and I wanted to hide a particular node for a specific condition. I thought this would be pretty easy and I might just have to set the "Visible" property of the node. But I was surprised to see that the node does not have this property maybe because the node is not a control.

If the treeview is built from a data source, you can use the TreeNodeDataBound event to acheive this. But its not as simple as doing
e.node.visible because as I said, visible is not a property

Even treeviewControl.Nodes.Remove(e.node) will not work if you are trying to remove a node that is a leaf node (node that does not have children).

So to remove a node, just try this code
e.Node.Parent.ChildNodes.Remove(e.Node);

No comments:

Post a Comment