windowsforms-5553-controls-and-libraries-tree-list-feature-center-data-binding.md
This section contains information on binding modes supported by the Tree List control.
Learn how to create a Tree List control, bind it to data and customize the control’s view and behavior options.
In this mode, the Tree List creates nodes for all records in the underlying data source once the data source is assigned to the control.
The Tree List control can be bound to any traditional data source: a BindingSource, BindingList<T>, List<T>, DataTable, DataView and DataSet objects, objects that implement specific interfaces (e.g., IList, ITypedList and IBindingList), SQL data, XML data, Excel data sources, etc.
The following example demonstrates how to bind the TreeList control to a BindingList source with Record objects. The Record class implements the INotifyPropertyChanged interface to notify the TreeList control that a property value has changed.
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.ComponentModel.DataAnnotations;
using DevExpress.XtraEditors;
namespace DXApplication1 {
public partial class Form1 : XtraForm {
BindingList<Record> records;
public Form1() {
InitializeComponent();
records = new BindingList<Record>() {
new Record(0){ ParentID = 0, JobTitle = "Chief Executive Officer", FirstName = "Bruce", LastName = "Cambell" },
new Record(1){ ParentID = 0, JobTitle = "Information Services Manager", FirstName = "Cindy", LastName = "Haneline" },
new Record(2){ ParentID = 1, JobTitle = "Database Administrator", FirstName = "Andrea", LastName = "Deville" },
new Record(3){ ParentID = 1, JobTitle = "Application Specialist", FirstName = "Anita", LastName = "Ryan" },
new Record(4){ ParentID = 0, JobTitle = "Network Manager", FirstName = "Dora", LastName = "Cardle" },
};
treeList1.DataSource = records;
treeList1.KeyFieldName = "ID";
treeList1.ParentFieldName = "ParentID";
textEdit1.DataBindings.Add(new Binding("EditValue", records, "JobTitle"));
textEdit1.Properties.ValidateOnEnterKey = true;
}
}
public class Record : INotifyPropertyChanged {
int id;
public Record(int id) {
this.id = id;
}
[Display(Order = -1)]
public int ID {
get { return id; }
}
[Display(Order = -1)]
public int ParentID { get; set; }
string jobTitle;
public string JobTitle {
get { return jobTitle; }
set {
if (jobTitle != value) {
if (string.IsNullOrEmpty(value))
throw new Exception();
jobTitle = value;
OnPropertyChanged();
}
}
}
string firstName;
public string FirstName {
get { return firstName; }
set {
if (firstName != value) {
firstName = value;
OnPropertyChanged();
}
}
}
string lastName;
public string LastName {
get { return lastName; }
set {
if (lastName != value) {
lastName = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Imports System
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports System.ComponentModel.DataAnnotations
Imports DevExpress.XtraEditors
Namespace DXApplication12
Partial Public Class Form1
Inherits XtraForm
Private records As BindingList(Of Record)
Public Sub New()
InitializeComponent()
records = New BindingList(Of Record)() From {
New Record(0) With {
.ParentID = 0,
.JobTitle = "Chief Executive Officer",
.FirstName = "Bruce",
.LastName = "Cambell"
},
New Record(1) With {
.ParentID = 0,
.JobTitle = "Information Services Manager",
.FirstName = "Cindy",
.LastName = "Haneline"
},
New Record(2) With {
.ParentID = 1,
.JobTitle = "Database Administrator",
.FirstName = "Andrea",
.LastName = "Deville"
},
New Record(3) With {
.ParentID = 1,
.JobTitle = "Application Specialist",
.FirstName = "Anita",
.LastName = "Ryan"
},
New Record(4) With {
.ParentID = 0,
.JobTitle = "Network Manager",
.FirstName = "Dora",
.LastName = "Cardle"
}
}
treeList1.DataSource = records
treeList1.KeyFieldName = "ID"
treeList1.ParentFieldName = "ParentID"
textEdit1.DataBindings.Add(New Binding("EditValue", records, "JobTitle"))
textEdit1.Properties.ValidateOnEnterKey = True
End Sub
End Class
Public Class Record
Implements INotifyPropertyChanged
Private id_Renamed As Integer
Public Sub New(ByVal id As Integer)
Me.id_Renamed = id
End Sub
<Display(Order := -1)>
Public ReadOnly Property ID() As Integer
Get
Return id_Renamed
End Get
End Property
<Display(Order := -1)>
Public Property ParentID() As Integer
Private jobTitle_Renamed As String
Public Property JobTitle() As String
Get
Return jobTitle_Renamed
End Get
Set(ByVal value As String)
If jobTitle_Renamed <> value Then
If String.IsNullOrEmpty(value) Then
Throw New Exception()
End If
jobTitle_Renamed = value
OnPropertyChanged()
End If
End Set
End Property
Private firstName_Renamed As String
Public Property FirstName() As String
Get
Return firstName_Renamed
End Get
Set(ByVal value As String)
If firstName_Renamed <> value Then
firstName_Renamed = value
OnPropertyChanged()
End If
End Set
End Property
Private lastName_Renamed As String
Public Property LastName() As String
Get
Return lastName_Renamed
End Get
Set(ByVal value As String)
If lastName_Renamed <> value Then
lastName_Renamed = value
OnPropertyChanged()
End If
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(Optional <CallerMemberName> ByVal propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
End Namespace
Read the following topic for detailed information: Bound Mode.
In virtual mode, data is loaded by the Tree List control on demand, using specific events or by calling certain methods on the bound object. Virtual mode significantly speeds up application performance for complex data sources with many parent-child nesting levels.
Virtual mode can be implemented using one of the two approaches:
See Also
Virtual Mode - Load Data Using Events
Virtual Mode - Binding to a Hierarchical Business Object (Data Source Level)