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

Treeview binding to model

1 Answer 45 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Tino
Top achievements
Rank 1
Tino asked on 04 May 2018, 04:22 AM

I have been populating my treeviews programmatically but would like to change to binding.

I read data from db and build a data model. I'd like to bind the data model to my treeview. so that when collections of objects and objects' properties themselves change they should automatically update in the treeview.

Which binding method would be the best way to achieve this? I've attached a simple mockup diagram to show the hierarchical structure. (Node types are abbreviated and I've removed the N level shown in the diagram from the code, as it doesn't really add to my question). The first part shows the model, and below shows some sample data.

I did a quick test like below which works, but doesn't allow for the collection of Table objects in Form (sibling to the Elements collection) to be populated. I hope this makes sense.

// pseudocode         
class DataModel { public List<Form> forms; }
class Form { public string Name { get; set;} public List<Element> Elements; public List<Table> Tables; }
class Element { public string Name { get; set;} }
class Column { public string Name { get; set;} }
class Table { public string Name { get; set;} public List<Column> Columns; }
            
// not showing creation of data model; see diagram for structure

// var DataModel sampleData;

_view.DataSource = new BindingList<Form>(sampleData.forms);
_view.DisplayMember = "Name\\Name";
_view.ChildMember = "Forms\\Elements";

 

As a side question, I'd like to have the T objects and the F objects (for example) in their own visual 'folder' in the treeview. Can this be still achieved with data binding, or should I stick my manual population method (which has lots of overhead trying to keep data model and tree view in synch with events, etc.).

 

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 May 2018, 09:46 AM
Hello, Tino,     

According to the provided pseudo code it seems that you have two nested collections (T and E) at a single level. It can't be extracted automatically by RadTreeView. The possible solution that I can suggest is to use load on demand hierarchy. Thus, you can control what nodes to be added considering the parent:
https://docs.telerik.com/devtools/winforms/treeview/data-binding/load-on-demand
https://docs.telerik.com/devtools/winforms/treeview/data-binding/load-on-demand-with-crud-operations  

An alternative solution is to use RadGridView which supports multiple templates at a single level. Additional information is available in the following help article:
https://docs.telerik.com/devtools/winforms/gridview/hierarchical-grid/hierarchy-of-one-to-many-relations

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Treeview
Asked by
Tino
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or