hide node in treeview control

14,335

Solution 1

I use Telerik RadTreeView; TreeView doesn't have DataBound event and Visible property for each node. Here is the code to remove the child node for TreeView.

protected void Page_Load(object sender, EventArgs e)
{
  RemoveNodeRecurrently(TreeView1.Nodes, "Status");
}

private void RemoveNodeRecurrently(TreeNodeCollection childNodeCollection, string text)
{
  foreach (TreeNode childNode in childNodeCollection)
  {
    if (childNode.ChildNodes.Count > 0)
      RemoveNodeRecurrently(childNode.ChildNodes, text);

    if (childNode.Text == text)
    {
      TreeNode parentNode = childNode.Parent;
      parentNode.ChildNodes.Remove(childNode);
      break;
    }
  }
}

Solution 2

You can try this, it works for Leaf Nodes only.

TreeView1.Nodes[0].Text = "";

TreeView1.Nodes[0].ShowCheckBox = false;

P.S: You will need a recursive function to access each node.

Share:
14,335
user175084
Author by

user175084

Updated on June 04, 2022

Comments

  • user175084
    user175084 almost 2 years

    I have a tree view created in my HTML Page

    <asp:TreeView ID="TreeView1" runat="server" 
               onselectednodechanged="TreeView1_SelectedNodeChanged" 
               PopulateNodesFromClient="False" onunload="TreeView1_Unload">
               <Nodes>
    
                   <asp:TreeNode Text="Reports" Value="Report">
    
                   <asp:TreeNode Text="Status" Value="Service">
                       </asp:TreeNode>
    
                       <asp:TreeNode Text="Status" Value="Status">
                       </asp:TreeNode>
    
                       <asp:TreeNode Text="Stats" 
                           Value="Stats"></asp:TreeNode>
    
                   </asp:TreeNode>
               </Nodes>
           </asp:TreeView>
    

    now i want to hide the Stats node in the page load function in my code behind....

    any suggestions.. thanks

  • Vincent Sels
    Vincent Sels about 11 years
    This doesn't seem to work with System.Windows.Forms.TreeView - the node's checkbox is still rendered.
  • marsh-wiggle
    marsh-wiggle about 8 years
    seems to need some explanation