This is a migrated thread and some comments may be shown as answers.

Hiding a tree node

5 Answers 1147 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Zoltán
Top achievements
Rank 1
Zoltán asked on 07 Feb 2012, 02:58 PM
Hello,

1. I'm having difficulties hiding a folder tree node.
Setting the visibility to collapsed of the NodeElement will make the rows of the tree overlapping each other. How can I resolve this?

private void SignalTreeView_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
        {
            DataRowView rowView = (DataRowView)e.Node.DataBoundItem;
            e.NodeElement.ImageElement.Image = (Image)rowView[TreeNodes.COL_ICON];
              
            TreeNode tn = rowView[TreeNodes.COL_ITEM] as TreeNode;
            if (tn is FolderNode && e.Node.Nodes.Count == 0)
            {
                e.NodeElement.Visibility = ElementVisibility.Collapsed;
            }
        }

See the attached image!

2. Another problem is that I'm not able to hide the folder nodes when all children are filtered out (using custom filter). Is there any way I can do this? Folders can be multilevel.

Best Regards,
Zoltán Nagy

5 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 10 Feb 2012, 09:40 AM
Hello Zoltán,

Thank you for writing and for the provided code snippet.

You should hide the actual data nodes and not the node elements. Your code should look like this:
private void SignalTreeView_NodeFormatting(object sender, TreeNodeFormattingEventArgs e) 
  DataRowView rowView = (DataRowView)e.Node.DataBoundItem; 
  e.NodeElement.ImageElement.Image = (Image)rowView[TreeNodes.COL_ICON];              
 
  TreeNode tn = rowView[TreeNodes.COL_ITEM] as TreeNode; 
 
  if (tn is FolderNode && e.Node.Nodes.Count == 0) 
  
    e.Node.Visible = false;
  
}

The above code fix should also fix the problem in your second question. I tested it and when I filter out all child nodes of a given node, the node formatting makes it invisible.

I hope this will help. If you have further questions, I would be glad to help.

All the best,
Ivan Petrov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Zoltán
Top achievements
Rank 1
answered on 10 Feb 2012, 03:38 PM
Hello Ivan,

Thanks for your answer!
Your solution is working, however I should use a TreeView which is inherited from RadTreeView and in this case it is not working, no nodes can be hidden.

Here is my code:
public partial class SignalTreeView : RadTreeView
    {
  
        private Point? lastMouseDownLocation;
  
        public SignalTreeView() : base ()
        {
            InitializeComponent();
        }
  
        public SignalTreeView(IContainer container)
        {
            container.Add(this);
  
            InitializeComponent();
        }
  
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadTreeView).FullName;
            }
        }
  
        private void SignalTreeView_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
        {
            DataRowView rowView = (DataRowView)e.Node.DataBoundItem;
            e.NodeElement.ImageElement.Image = (Image)rowView[TreeNodes.COL_ICON];
              
            TreeNode tn = rowView[TreeNodes.COL_ITEM] as TreeNode;
            if (tn is FolderNode && e.Node.Nodes.Count == 0)
            {
                e.Node.Visible = false;
            }
        }
  
        private void SignalTreeView_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(List<ListViewDataItem>)))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
  
        private void SignalTreeView_MouseDown(object sender, MouseEventArgs e)
        {
            RadElement itemAtPoint = ElementTree.GetElementAtPoint(e.Location);
            if (itemAtPoint != null && !(itemAtPoint is ScrollBarThumb) && !(itemAtPoint is ScrollBarButton))
                this.lastMouseDownLocation = new Point(e.X, e.Y);
            else
                this.lastMouseDownLocation = null;
        }
  
        private void SignalTreeView_MouseMove(object sender, MouseEventArgs e)
        {
            Point cursorPos = new Point(e.X, e.Y);
            if (e.Button == MouseButtons.Left && this.lastMouseDownLocation != null && SelectedNodes.Count > 0)
            {
                Point dragPoint = (Point)this.lastMouseDownLocation;
  
                Rectangle dragRect = new Rectangle(dragPoint, SystemInformation.DragSize);
                dragRect.X -= (int)(SystemInformation.DragSize.Width / 2);
                dragRect.Y -= (int)(SystemInformation.DragSize.Height / 2);
  
                if (!dragRect.Contains(cursorPos))
                {
                    Capture = false;
  
                    List<RadTreeNode> treeNodes = new List<RadTreeNode>(SelectedNodes);
                    DoDragDrop(treeNodes, DragDropEffects.Copy);
                }
            }
        }
  
    }


Any ideas?

Best regards,
Zoltán Nagy
0
Ivan Petrov
Telerik team
answered on 15 Feb 2012, 12:02 PM
Hello Zoltán,

Thank you for your reply.

When deriving from any RadControl, the best practice is to create a new class that inherits from the RadControl. I have created a sample project using your code which follows the above practice. I have commented some code lines as I do not have the full source code. In the project you will find a form with a tree view and two buttons. One fills the tree nodes the second hides all children of the second node in the tree. Than the node formatting code is executed and the node with no children is hidden.

I hope this will be useful for you. Should you have further questions feel free to write back.

Kind regards,
Ivan Petrov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Zoltán
Top achievements
Rank 1
answered on 15 Feb 2012, 02:14 PM
Hello Ivan,

Thanks for the example project, now I see where the problem might be. It is not connected with inheritance, it is connected with data binding I think.
Please change in your example project in Form1.cs to the following:
private void radButtonFillNodes_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable(); 
              
            table.Columns.Add("ID", typeof(int)); 
            table.Columns.Add("ParentID", typeof(int)); 
            table.Columns.Add("Name", typeof(string)); 
            table.Columns.Add("Title", typeof(string)); 
            table.Columns.Add("Icon", typeof(Image)); 
            table.Columns.Add("IsNew", typeof(bool)); 
            table.Columns.Add("NewItemCount", typeof(int)); 
            table.Columns.Add("IsImportant", typeof(bool)); 
            table.Columns.Add("HasFlag", typeof(bool)); 
            table.Rows.Add(0, null, "Personal Folders", null, null); 
            table.Rows.Add(1, 0, "Deleted Items", null, null, false, 1); 
            table.Rows.Add(2, 0, "Drafts", null, null);
            table.Rows.Add(3, 0, "Inbox", null, null, false, 3);
            table.Rows.Add(4, 0, "Junk E-mails", null, null);
            table.Rows.Add(5, 0, "Outbox", null, null);
            table.Rows.Add(6, 0, "Send Items", null, null);
            table.Rows.Add(7, 0, "Search Folder", null, null);
            table.Rows.Add(8, 1, "Adam Smith", "You`ve got to see this!", null, true);
            table.Rows.Add(9, 3, "Lewis Clark", "This is extremely urgent", null, true, null, true);
            table.Rows.Add(10, 3, "Tomas Brown", "Need your help with this article", null, false, null, false, true);
            table.Rows.Add(11, 3, "Jeff Patel", "Please, check this our and report by Tomorow!", null, true);
            table.Rows.Add(12, 3, "Smith Jones", "Seend this yet?", null, true);
            table.Rows.Add(13, 3, "Denis Cooper", "Great new tool", null, false);
            table.Rows.Add(14, 3, "Jackie Turner", "Team Building Session - All Hands", null, false, null, true);
            this.signalTreeView1.DataSource = table;
            this.signalTreeView1.DisplayMember = "Name";
            this.signalTreeView1.ChildMember = "ID";
            this.signalTreeView1.ParentMember = "ParentID"
  
        }
  
private void radButtonClearChildren_Click(object sender, EventArgs e)
        {
            this.signalTreeView1.Nodes[0].Nodes[2].Nodes.Clear();
        }

So when I use DataTable with DataBinding hiding is not working. Do you have any idea why?

Best regards,
Zoltán Nagy
0
Ivan Petrov
Telerik team
answered on 20 Feb 2012, 09:29 AM
Hi Zoltán,

Thank you for getting back to us.

I copy-pasted the code you have sent me in your last reply. I added an event handler for the NodeFormatting event of the signal tree view with the following code:
private void signalTreeView1_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
{
  if (e.Node.Text == "Inbox" && e.Node.Nodes.Count == 0)
  {
    e.Node.Visible = false;
  }
}

 Everything worked as expected. When I hit the button that hides the child nodes of the "Index" node, the "Index" node gets hidden by the code in the NodeFormatting event handler. Please, refer to the attached video for the behavior on my side along with the modified project.

Should you need further assistance, I would be glad to provide it.
 
All the best,
Ivan Petrov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Treeview
Asked by
Zoltán
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Zoltán
Top achievements
Rank 1
Share this question
or