Data Binding in Windows Forms TreeView
The following are the ways to bind various data sources to TreeViewAdv.
- Binding to Self-Referencing Data
- Binding to Data Relations
- Binding to Object-Relational Data
Binding to Self-Referencing Data
Parent-Child relationship for all the records is defined by setting the ParentMember and ChildMember properties to the respective fields in the data source. If the Parent ID of one record has the respective value in the Child ID of any other records from the table, then that record is considered to have parent. If not, then the record is considered to have no parents and, in such case, it won’t be visible in the TreeViewAdv.
|DataSource||Gets or sets the data source object in TreeViewAdv.|
|DisplayMember||Gets or sets the field that populates the Text property of the TreeNodeAdv in TreeViewAdv.|
|ValueMember||Gets or sets the field that populates the Value property of TreeNodeAdv in TreeViewAdv.|
|ParentMember||Gets or sets a value indicating the parent data field which behaves as a parent node in TreeViewAdv.|
|ChildMember||Gets or sets a value indicating the child data field which behaves as a child node in TreeViewAdv.|
|DataMember||Gets or sets the table name that should be bound to the TreeViewAdv.|
|CheckedMember||Gets or sets a value indicating the field to populate the state of checkbox of TreeNodeAdv in TreeViewAdv.|
|SelfRelationRootValue||Gets or sets the value that defines the root object in a self-relational mode when data source is used to define the underlying tree data.|
In the above table, North America is declared as root node by setting appropriate (or matching) SelfRelationRootValue. Here the value is set to empty and so North America is added as root node.
United States is declared as child of North America by setting appropriate ContinentID. Here the value is set to 1 and it matches the CountryID of North America. As a result, United States is added as child node of North America.
treeViewAdv1.SelfRelationRootValue = ""; treeViewAdv1.DisplayMember = "Name"; treeViewAdv1.ParentMember = "ContinentID"; treeViewAdv1.ChildMember = "CountryID"; treeViewAdv1.ValueMember = "Capital"; treeViewAdv1.CheckedMember = "IsActive"; treeViewAdv1.DataSource = Table_1;
treeViewAdv1.SelfRelationRootValue = "" treeViewAdv1.DisplayMember = "Name" treeViewAdv1.ParentMember = "ContinentID" treeViewAdv1.ChildMember = "CountryID" treeViewAdv1.ValueMember = "Capital" treeViewAdv1.CheckedMember = "IsActive" treeViewAdv1.DataSource = Table_1
1) Root nodes are parent less (for example, see “North America” node in the image above). All root nodes must have their parent member values (TreeViewAdv.ParentMember) set to TreeViewAdv.SelfRelationRootValueproperty. If the property is not specified then, you may notice empty TreeViewAdv. So, to avoid this, we suggest that you always set parent member values with TreeViewAdv.SelfRelationRootValue for root nodes.
2) You must specify TreeViewAdv.DisplayMember, TreeViewAdv.ParentMember, TreeViewAdv.ChildMember properties mandatorily to populate the appropriate TreeViewAdv.
Binding to Data Relations
In this type, TreeViewAdv are bound to database where levels are created using DataRelation class. Each DataRelation class object describes the parent data being bound to, the child data being bound to and the data columns used to populate Text and selected Value properties of the nodes.
|1||DataRelations||Contains the collection of DataRelation in TreeViewAdv.|
|1||DataRelation||Represents a parent/child relationship between two DataTable objects.|
Syncfusion.Windows.Forms.Tools.DataRelation childRelation1; Syncfusion.Windows.Forms.Tools.DataRelation childRelation2; Syncfusion.Windows.Forms.Tools.DataRelation childRelation3; childRelation1 = new Syncfusion.Windows.Forms.Tools.DataRelation(Table_2, "Table_2", "SubFolderName1", "ChildFolder", "SubFolderChild1", "SubFolderName1", "Checked"); childRelation2 = new Syncfusion.Windows.Forms.Tools.DataRelation(Table_3, "SubFolderName2", "SubFolderChild1", "SubFolderChild2"); childRelation3 = new Syncfusion.Windows.Forms.Tools.DataRelation(Table_4, "Table_4", "SubFolderName3", "SubFolderChild2", "SubFolderChild3", "SubFolderName3", "Checked"); treeViewAdv1.DataRelations.Clear(); treeViewAdv1.DisplayMember = "FolderName"; treeViewAdv1.ParentMember = "ParentFolder"; treeViewAdv1.ChildMember = "ChildFolder"; treeViewAdv1.DataRelations.Add(childRelation1); treeViewAdv1.DataRelations.Add(childRelation2); treeViewAdv1.DataRelations.Add(childRelation3); treeViewAdv1.DataSource = Table_1;
Dim childRelation1 As Syncfusion.Windows.Forms.Tools.DataRelation Dim childRelation2 As Syncfusion.Windows.Forms.Tools.DataRelation Dim childRelation3 As Syncfusion.Windows.Forms.Tools.DataRelation childRelation1 = New Syncfusion.Windows.Forms.Tools.DataRelation(Table_2, "SubFolder1", "SubFolderName1", "FolderChild", "SubFolderChild1", "SubFolderName1", "Checked") childRelation2 = New Syncfusion.Windows.Forms.Tools.DataRelation(Table_3, "SubFolderName2", "SubFolderChild1", "SubFolderChild2") childRelation3 = New Syncfusion.Windows.Forms.Tools.DataRelation(Table_4, "SubFolder3", "SubFolderName3", "SubFolderChild2", "SubFolderChild3", "SubFolderName3", "Checked") treeViewAdv1.DataRelations.Clear() treeViewAdv1.DisplayMember = "FolderName" treeViewAdv1.ParentMember = "ParentFolder" treeViewAdv1.ChildMember = "ChildFolder" treeViewAdv1.DataRelations.Add(childRelation1) treeViewAdv1.DataRelations.Add(childRelation2) treeViewAdv1.DataRelations.Add(childRelation3) treeViewAdv1.DataSource = Table_1
1) In this type, you must specify DisplayMember, ParentMember, ChildMember properties mandatorily to populate appropriate TreeViewAdv.
2) Here levels are created using DataRelation, so if you want to dynamically add new level then you need to create new instance of DataRelation and add it to TreeViewAdv.DataRelations property with proper relation first.
Binding to Object-Relational Data
In this type, TreeViewAdv binds to class objects which have collections associated with other dependent class objects establishing proper relation.
In this illustration, there are three classes named Continent, Country and State. In this case the Continent class has a property named ContinentName, ContinentID and Country_List which is a collection of Country objects. This is the first relation. The second relation is the State_List property in the Country class, it offers a reference to a collection of State.
|1||DisplayMember||Defines which property of the class needs to be in node's text. Example: Continent -> ContinentName, Country -> CountryName, State -> StateName.|
|2||ChildMember||Defines the class order. Example: Continent -> First level, Country -> Second level, State -> Third level.|
treeViewAdv1.DisplayMember = "ContinentName\\CountryName\\StateName"; treeViewAdv1.ChildMember = "Continent\\Country\\State";
treeViewAdv1.DisplayMember = "ContinentName\\CountryName\\StateName" treeViewAdv1.ChildMember = "Continent\\Country\\State"