Expanding and collapsing databound tree nodes by condition at startup

6 posts, 1 answers
  1. Falk Wegener
    Falk Wegener avatar
    28 posts
    Member since:
    Oct 2007

    Posted 12 Apr 2011 Link to this post

    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
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 14 Apr 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 15 Apr 2011 Link to this post

    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
  5. Falk Wegener
    Falk Wegener avatar
    28 posts
    Member since:
    Oct 2007

    Posted 19 Apr 2011 Link to this post

    Thank you Richard,

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

    Falk Wegener
    orgAnice Software GmbH
  6. Falk Wegener
    Falk Wegener avatar
    28 posts
    Member since:
    Oct 2007

    Posted 19 Apr 2011 Link to this post

    Hello Richard,
    Hello Jack,

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

    Falk Wegener
    orgAnice Software GmbH
  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 19 Apr 2011 Link to this post

    Hello,

    I'm glad that helped. Please remember to mark as answer so others can find the solution too.
    All the best
    Richard
Back to Top
UI for WinForms is Visual Studio 2017 Ready