Flexible TreeView Flexible TreeView


Support & Community

Data binding. Get started

Previous Table of Contents Next

Besides filling the treeview manually, Flexible TreeView allows filling it from the external data source including hierarchical as well as the plain one.

The following types of data source are supported:

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

Flexible TreeView supports both one-way and bidirectional (two-way) binding modes. Bidirectional binding mode allows to synchronize the changes of the treeview and data source without writing a single line of code.



DataBinding

All of the 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 – points out whether the treeview should save all of the data changes in the data source (bound mode) or data from the data source will be copied in to the treeview, and all of the changes made to it will not be saved in 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 unique identifier of the object is stored;
  • ParentFieldName – name of the field in the data source object, where 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

In order to bind data you need to point to your data source in the DataSource property:

tree.DataBinding.DataSource = list;

If you already pointed to your data source earlier, then made changes to it from the outside and want to refresh the treeview, call method DataBinding.Refresh.


BoundMode

Flexible TreeView supports two modes for working with data source – bound and unbound.
If the bound mode is on, nodes of the treeview will store only references to the objects shown in the treeview. At the same time during edit of the data in the treeview, the data source will also be renewed.  In order to turn on this mode set the value of the BoundMode property to TRUE.

If the unbound mode is on, nodes of the treeview will store in themselves data of the bound objects. At the same time during edit of the data in the treeview, the data source wouldn`t be renewed.  In order to turn on this mode set the value of the BoundMode property to FALSE.


BidirectionalMode

By default (BidirectionalMode = false), Flexible TreeView does no 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.
Also, Flexible TreeView allows to synchronize the changes of the treeview and data source without writing a single line of code. In this case appropriate data objects will be automatically created, deleted or changed in the bound data source.
To do that you need to enable the BidirectionalMode property and follow the demands specified in the topic Bidirectional binding mode.


KeyFieldName / ParentFieldName

Flexible TreeView supports binding of the plain data sources as well as the hierarchical ones.
If you want to show all objects as root nodes (plain data source), set the value of the KeyFieldName and ParentFieldName properties of the DataBinding object to NULL.
If the data source has data describing a hierarchy of objects (Id/ParentId), Flexible TreeView can build a treeview based solely on such data. In order to do that you need to point to the names of the properties which are storing Id of the object and Id of the parent object in the KeyFieldName and ParentFieldName properties of the DataBinding object respectively.  This id's can be any type.
The property, indicated by ParentFieldName, must be a reference or Nullable types so that there would be an opportunity to specify a NULL value for the objects represented on the root level of the tree.

Note, that if a KeyFieldName and ParentFieldName are filled, Flexible TreeView will not show these properties in the treeview!


ReadOnly

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


Validate

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

See the Data binding inspection topic for details.


BindableNode

You usually operate with instances of the ARMSoft.FlexibleTreeView.Nodes.Node class or with the inheritors in the treeview where there is no data source. In case when data source is bound to the treeview, each generated node of such a treeview is an instance of the ARMSoft.FlexibleTreeView.Nodes.BindableNode class derived from the Node class.

public class BindableNode : Node, IIndexable<object>

So, if you are working with nodes in the tree that`s bound to a data source, you have 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 the node (unbound or bound to a data source object) is added into a treeview:

void tree_NodePopulating(FlexibleTreeView pTreeview, NodePopulatingEventArgs pArgs)
{
  BindableNode node = (BindableNode) pArgs.Node;
  object dataSourceObject = node.BoundObject;
 
  // data source is a DataTable instance.
  DataRowView rowView = (DataRowView) dataSourceObject;
  // use bound object here.
}

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 is the example of how you can get this object if you bound DataTable to the treeview:

void tree_NodePopulating(FlexibleTreeView pTreeview, NodePopulatingEventArgs pArgs)
{
  BindableNode node = (BindableNode) pArgs.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";
}

Also, BindableNode class contains the Id property, which contains a bound object identifier if data source is hierarchical (the DataBinding.KeyFieldName and DataBinding.ParentFieldName properties are filled in). As it is shown in the next section, this value can be used to find a node in a treeview by identifier of a data source object:

 

Search nodes in bound mode

Every object in a hierarchical data source has a unique identifier. By using it you can find the node, which shows data for this object in the treeview. In this case you should use the FindChildNodeById node method:

object targetObjectId = "1";
BindableNode node = tree.Root.FindChildNodeById<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 of the data source (DataBinding.DataSource property) by raising the DataSourceChanged treeview event.

Previous Top Next


Last news
Bookmark and Share
Use Flexible TreeView in WPF project
Sunday, 18 December 2011

Do you have a WPF project and want to use Flexible TreeView there? No problem!

Flexible TreeView v3.4
Friday, 16 December 2011

Flexible TreeView v3.4 maintenance release has been released.

Flexible TreeView v3.3
Friday, 23 September 2011

Flexible TreeView v3.3 maintenance release has been released. Separate assemblies for .NET 2.0, 4.0 and 4.0 Client Profile, HTML markup extension, etc.

Latest release

Version:
Release date:
3.4
Dec 16, 2011


Copyright © 2006-2012 ARMSoft. All rights reserved.