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