Databinding to multilevel Generic List

2 posts, 0 answers
  1. Claus
    Claus avatar
    1 posts
    Member since:
    Jan 2014

    Posted 13 Feb 2014 Link to this post


    I have the following objects and datastructure that I would like to bind to the TreeView and have displayed. The classes and datastructure is simplified, but I'm sure you'll get the idea.

    public class Folder : TreeComponent
        public int Id { get; set; }
        public int ParentId { get; set; }
        public string Name { get; set; }
        public List<TreeComponent> Children { get; set; }
    public class File : TreeComponent
        public int Id { get; set; }
        public int ParentId { get; set; }
        public string Name { get; set; }

    Now, I have a generic list of List<TreeComponent> in which I have some "Folders" and some "Files". The "Folders" also contain "Subfolders" and other "Files". Graphically it looks something like this:

    -Folder1 (level1)
        - Folder2 (level2
            - File1 (level 3)
        - File2 (level2)
    - Folder3 (level1)

    So with that datastructure I bind to the TreeView:

    this.RadTreeViewAreas.DataFieldID = "Id";
    this.RadTreeViewAreas.DataFieldParentID = "ParentId";
    this.RadTreeViewAreas.DataTextField = "Name";
    this.RadTreeViewAreas.DataSource = this.treeComponentList

    ...but only "level 1" nodes are displayed. I guess the TreeView does not support lists with childlists. What is the easiest way to display those subfolders and files. It's not an obtion to "flatten" the initial list it has to stay like it is!

    Best regards

  2. Bozhidar
    Bozhidar avatar
    1096 posts

    Posted 17 Feb 2014 Link to this post

    Hi Claus,

    When binding declaratively, the TreeView uses the FieldID and FieldParentID properties to create the hierarchy from a flat list. Unfortunately there isn't a way to bind it declaratively to a hierarchical list like the one you specified, where the children are stored in an inner collection. In this case to bind the treeview you would have to manually traverse the list and construct the treeview.

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
  3. DevCraft R3 2016 release webinar banner
Back to Top