Treeview binding to model

2 posts, 0 answers
  1. Tino
    Tino avatar
    86 posts
    Member since:
    Jul 2017

    Posted 03 May 2018 Link to this post

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

     

     

  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3414 posts

    Posted 07 May 2018 Link to this post

    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.
Back to Top