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

Databinding to multilevel Generic List

1 Answer 48 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Claus
Top achievements
Rank 1
Claus asked on 13 Feb 2014, 10:06 AM
Hello!

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)
...etc.

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
this.RadTreeViewAreas.DataBind();

...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

Claus

1 Answer, 1 is accepted

Sort by
0
Bozhidar
Telerik team
answered on 17 Feb 2014, 08:25 AM
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.

Regards,
Bozhidar
Telerik
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.
Tags
TreeView
Asked by
Claus
Top achievements
Rank 1
Answers by
Bozhidar
Telerik team
Share this question
or