how to bind to a recursive object?

3 posts, 1 answers
  1. Bruce
    Bruce avatar
    25 posts
    Member since:
    May 2017

    Posted 25 Jul Link to this post

    I would like to be able to use a BindlingList of Node as a datasource for the treeview, but I'm unsure of how to set up the properties, Children in Node will contain Children, and so forth.   

    class Node
    {
      public string Title; //DisplayMember
      public string Value; //ValueMember
      public bool IsActive; //CheckedMember
      public List<Node> Children = new List<Node>(); //Children of this node here
       
      public Node()
      {
         
      }
       
      public Node(string title, string value, bool isActive, List<Node> children)
      {
        Title = title;
        Value = value;
        IsActive = isActive;
        Children = children;
      }
    }
  2. Answer
    Dess
    Admin
    Dess avatar
    2107 posts

    Posted 26 Jul Link to this post

    Hello Bruce, 

    Thank you for writing.  

    Here is a sample code snippet demonstrating how to bind RadTreeView to recursive data:
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            List<DBTTreeItem> treeSource = new List<DBTTreeItem>();
     
            for (int i = 0; i < 5; i++)//first level
            {
                List<DBTTreeItem> children = new List<DBTTreeItem>();
                treeSource.Add(new DBTTreeItem(i, "Description" + i, children));
                for (int j = 5; j < 10; j++)//second level
                {
                    List<DBTTreeItem> subChildren = new List<DBTTreeItem>();
                    children.Add(new DBTTreeItem(i * 10 + j, "Child description" + (i * 10 + j), subChildren));
                    if (j % 2 == 0)
                    {
                        for (int k = 10; k < 15; k++)//third level
                        {
                            subChildren.Add(new DBTTreeItem(i + j + k, "Sub child description" + (i + j + k), new List<DBTTreeItem>()));
                        }
                    }
                }
            }
     
            radTreeView1.DataSource = treeSource;
            radTreeView1.DisplayMember = "Description\\Description\\Description";
            radTreeView1.ChildMember = "Children\\Children\\Children";
        }
     
        public void AdjustMemberMapping()
        {
            List<DBTTreeItem> treeSource = radTreeView1.DataSource as List<DBTTreeItem>;
            StringBuilder desc = new StringBuilder();
            StringBuilder child = new StringBuilder();
            int depth = 4;
            treeSource[0].Children[1].Children[0].Children.Add(new DBTTreeItem(123, "New added" + 123, new List<DBTTreeItem>()));
     
            for (int i = 0; i < depth; i++)
            {
                if (desc.Length > 0)
                {
                    desc.Append('\\');
                    child.Append('\\');
                }
                desc.Append("Description");
                child.Append("Children");
            }
     
            radTreeView1.DataSource = treeSource;
            radTreeView1.DisplayMember = desc.ToString();
            radTreeView1.ChildMember = child.ToString();
        }
     
        private void radButton1_Click(object sender, EventArgs e)
        {
            AdjustMemberMapping();
        }
    }
     
    public class DBTTreeItem
    {
        public int ID { get; set; }
     
        public string Description { get; set; }
     
        public List<DBTTreeItem> Children { get; set; }
     
        public DBTTreeItem(int iD, string description, List<DBTTreeItem> children)
        {
            this.ID = iD;
            this.Description = description;
            this.Children = children;
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Bruce
    Bruce avatar
    25 posts
    Member since:
    May 2017

    Posted 26 Jul in reply to Dess Link to this post

    Thanks this helps a lot. 
Back to Top