Skip to content

Data binding. Get started#

Besides filling the treeview manually, Flexible TreeView allows filling it from an external data source, including both hierarchical and plain data structures.

The following types of data sources are supported:

  • IEnumerable
  • IList
  • IListSource
  • ITypedList
  • IBindingList
  • IBindingListView
  • DataTable

Flexible TreeView supports both one-way and bidirectional binding modes. Bidirectional binding mode allows synchronization of changes between the treeview and data source without writing a single line of code.

DataBinding#

All settings for binding data can be found in the DataBinding treeview property, which includes the following options:

  • DataSource - points to the external data source.
  • BoundMode - determines whether the treeview should save all data changes in the data source (bound mode) or if data from the data source will be copied into the treeview, with changes not saved back to the data source (unbound mode).
  • BidirectionalMode - determines whether to synchronize the content of a treeview and bound data source when you make changes (add and/or delete objects/nodes) in them.
  • KeyFieldName - name of the field in the data source object where the unique identifier of the object is stored.
  • ParentFieldName - name of the field in the data source object where the identifier of the parent object is stored.
  • ReadOnly - defines whether the user can edit data in the treeview.
  • Validate - defines whether treeview notifies about created objects when binding a data source.

DataSource#

To bind data, you need to point to your data source in the DataSource property:

treeview.DataBinding.DataSource = list;
treeview.DataBinding.DataSource = list

If you've already pointed to your data source earlier and made changes to it externally, call the treeview.DataBinding.Refresh method to refresh the treeview.

BoundMode#

Flexible TreeView supports two modes for working with data sources: bound and unbound.

  • Bound mode: Nodes of the treeview will store only references to the objects shown in the treeview. During editing of the data in the treeview, the data source will also be updated. To turn on this mode, set the value of the DataBinding.BoundMode treeview property to true.

  • Unbound mode: Nodes of the treeview will store the data of the bound objects internally. During editing of the data in the treeview, the data source won't be updated. To turn on this mode, set the value of the DataBinding.BoundMode treeview property to false.

BidirectionalMode#

By default (BidirectionalMode = false), Flexible TreeView does not perform any actions if the treeview structure or data source has been changed (a node has been added or deleted), i.e., the one-way data binding mode is activated.

Flexible TreeView also allows synchronization of changes between the treeview and data source without writing code. In this case, appropriate data objects will be automatically created, deleted, or changed in the bound data source.

To enable this, set the BidirectionalMode property to true and follow the requirements specified in the Bidirectional binding mode topic.

KeyFieldName / ParentFieldName#

Flexible TreeView supports binding of both plain and hierarchical data sources.

For plain data sources: Set the value of the KeyFieldName and ParentFieldName properties of the DataBinding treeview property to null to show all objects as root nodes.

For hierarchical data sources: If the data source describes a hierarchy of objects (Id/ParentId), Flexible TreeView can build a treeview based solely on such data. Point to the names of the properties storing the Id of the object and Id of the parent object in the KeyFieldName and ParentFieldName properties of the DataBinding object, respectively. These IDs can be of any type.

The property indicated by ParentFieldName must be a reference or Nullable type to allow specifying a null value for objects represented at the root level of the tree.

Tip

If the KeyFieldName and ParentFieldName properties are filled, Flexible TreeView will not bind these properties to the treeview!

ReadOnly#

By default, users can edit data after binding. You can deny editing of the data by setting the value of the DataBinding.ReadOnly property to true.

Validate#

During binding of the data source, all public properties (as columns and node controls) and objects (as nodes) located in the data source will be added to the treeview. Flexible TreeView also allows you to perform validation of these added objects. To do this, turn on the Validate property of the DataBinding object and subscribe to the ColumnPopulating, NodeControlPopulating, and NodePopulating treeview events.

See the Data binding inspection topic for details.

BindableNode#

Usually, you operate with instances of the Node class or its inheritors in a treeview that is not bound to a data source. When a data source is bound to the treeview, each generated node is an instance of the BindableNode class derived from the Node class.

public class BindableNode : Node, IIndexable<object>
{
  // Implementation...
}
Public Class BindableNode
    Inherits Node
    Implements IIndexable(Of Object)
' Implementation...
End Class

When working with nodes in a tree bound to a data source, you need to cast these nodes to the BindableNode type.

This example shows how to get a generated node in the NodePopulating event handler, which notifies when a node (unbound or bound to a data source object) is added to the treeview:

void tree_NodePopulating(FlexibleTreeView treeview, NodePopulatingEventArgs args)
{
  BindableNode node = (BindableNode) args.Node;
  object dataSourceObject = node.BoundObject;

  // data source is a DataTable instance.
  DataRowView rowView = (DataRowView) dataSourceObject;
  // use bound object here.
}
Private Sub tree_NodePopulating(treeview As FlexibleTreeView, args As NodePopulatingEventArgs)
    Dim node As BindableNode = DirectCast(args.Node, BindableNode)
    Dim dataSourceObject As Object = node.BoundObject

    ' data source is a DataTable instance.
    Dim rowView As DataRowView = DirectCast(dataSourceObject, DataRowView)
    ' use bound object here...
End Sub

The BindableNode class contains the BoundObject property, which refers to an object from the data source with data being displayed by this node in the treeview.

This example shows how to get this object if you bound a DataTable to the treeview:

void tree_NodePopulating(FlexibleTreeView treeview, NodePopulatingEventArgs args)
{
  BindableNode node = (BindableNode) args.Node;

  // get the bound object.
  object dataSourceObject = node.BoundObject;

  // data source is a DataTable instance.
  DataRowView rowView = (DataRowView) dataSourceObject;
  // use bound object here.
  rowView.Row["text_column"] = "default value";
}
Private Sub tree_NodePopulating(treeview As FlexibleTreeView, args As NodePopulatingEventArgs)
    Dim node As BindableNode = DirectCast(args.Node, BindableNode)

    ' get the bound object.
    Dim dataSourceObject As Object = node.BoundObject

    ' data source is a DataTable instance.
    Dim rowView As DataRowView = DirectCast(dataSourceObject, DataRowView)
    ' use bound object here.
    rowView.Row("text_column") = "default value"
End Sub

The BindableNode class also contains the Id property, which holds a bound object identifier if the data source is hierarchical (the DataBinding.KeyFieldName and DataBinding.ParentFieldName properties are filled in). This value can be used to find a node in a treeview by the identifier of a data source object.

Search nodes in bound mode#

Every object in a hierarchical data source has a unique identifier. You can use it to find the node that shows data for this object in the treeview. Use the FindChildNodeById node method:

object targetObjectId = "1";
BindableNode node = tree.Root.FindChildNodeById<BindableNode, object>(targetObjectId);
Dim targetObjectId As Object = "1"
Dim node As BindableNode = tree.Root.FindChildNodeById(Of BindableNode, Object)(targetObjectId)

In your code, you can also use the Id property of the BindableNode class, which contains a bound object identifier.

DataSourceChanged event#

The treeview will notify you about changes to the data source (DataBinding.DataSource property) by raising the DataSourceChanged treeview event.