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

Expanding and collapsing databound tree nodes by condition at startup

5 Answers 90 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Technik
Top achievements
Rank 1
Technik asked on 12 Apr 2011, 03:55 PM
Hello,

in our scenario we have a treeview bound with self referencing items.
The goal is to serialize the expansion node states when leaving the application and restoring them on resumption.
While the serialization works just fine we have run into an issue trying to expand and/or collapse nodes when they get created.
On  first glance the NodeFormatting event seemed promising since it gets fired at the right time.
Unfortunately any manipulation inside its handler, no matter how small, causes the node not to be shown in the tree, thus leaving our tree empty, since all nodes get formatted this way. We currently set this issue aside, but need to implement it at some point in the future.
Any help is greatly appreciated.

Falk Wegener
orgAnice Software GmbH

5 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 14 Apr 2011, 10:32 AM
Hello,

You can do this by subscribing to the CreateNodeElement event .

The following sample just assumes you have a RadTreeView docked on a standard windows form. It will expand the node if the person's role is manager. Otherwise it will leave it collapsed.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace RadControlsWinFormsApp1
{
    public partial class Form1 : Form
    {
        private BindingList<Person> people = new BindingList<Person>();
  
        public Form1()
        {
            InitializeComponent();
  
            people.Add(new Person(1, "Richard", "Manager", 0));
            people.Add(new Person(2, "Chris", "Manager", 0));
            people.Add(new Person(3, "Leisl", "Manager", 0));
  
            people.Add(new Person(4, "Bob", "Junior Manager", 1));
            people.Add(new Person(5, "Stew", "Jumior Manager", 2));
            people.Add(new Person(6, "Peter", "Junior Manager", 3));
  
            people.Add(new Person(4, "Ian", "Staff", 4));
            people.Add(new Person(5, "Mike", "Staff", 5));
            people.Add(new Person(6, "Ester", "Staff", 6));
  
            this.radTreeView1.ShowLines = true;
            this.radTreeView1.CreateNodeElement += new Telerik.WinControls.UI.CreateTreeNodeElementEventHandler(radTreeView1_CreateNodeElement);
  
            this.radTreeView1.DataSource = people;
            this.radTreeView1.DisplayMember = "Name";
            this.radTreeView1.ValueMember = "Id";
            this.radTreeView1.ParentMember = "ParentId";
            this.radTreeView1.ChildMember = "Id";
        }
  
        void radTreeView1_CreateNodeElement(object sender, Telerik.WinControls.UI.CreateTreeNodeElementEventArgs e)
        {
            if (e.Node != null)
            {
                Person person = e.Node.DataBoundItem as Person;
                if (person != null)
                {
                    if (person.Role == "Manager")
                    { e.Node.Expanded = true; }
                    else
                    { e.Node.Expanded = false; }
                }
            }
        }
  
    }
  
    public class Person
    {
        public Person(int id, string name, string role, int parentId)
        {
            this.Id = id; this.Name = name; this.Role = role; this.ParentId = parentId;
        }
  
        public int Id
        { get; set; }
  
        public string Name
        { get; set; }
  
        public string Role
        { get; set; }
  
        public int ParentId
        { get; set; }
    }
}

Hope that helps but let me know if you need more information
Richard
0
Jack
Telerik team
answered on 15 Apr 2011, 09:17 AM
Richard, thank you for this solution.

Hello Falk, please check the solution from Richard and tell whether it fits in your case. RadTreeView control uses UI virtualization and the NodeFormatting event is called only for nodes that are currently visible.

Should you have any further questions, we will be glad to help.
 
Regards,
Jack
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Technik
Top achievements
Rank 1
answered on 19 Apr 2011, 09:40 AM
Thank you Richard,

we will try your approach and get back to you as soon as we have results.

Falk Wegener
orgAnice Software GmbH
0
Technik
Top achievements
Rank 1
answered on 19 Apr 2011, 10:38 AM
Hello Richard,
Hello Jack,

I implemented the advised solution and everything works as expected, thank you very much.

Falk Wegener
orgAnice Software GmbH
0
Richard Slade
Top achievements
Rank 2
answered on 19 Apr 2011, 10:46 AM
Hello,

I'm glad that helped. Please remember to mark as answer so others can find the solution too.
All the best
Richard
Tags
Treeview
Asked by
Technik
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Jack
Telerik team
Technik
Top achievements
Rank 1
Share this question
or