Data binding. Prepare data source#
Supported Data Types#
Flexible TreeView supports binding of the following primitive data types:
- bool, Nullable
or ARMSoft.FlexibleTreeView.Nodes.eCheckState - int, long, decimal, float, double or byte
- string
- DateTime
- System.Drawing.Color
- System.Drawing.Image and all its descendants (32-bit per pixel)
During binding of the data source to the treeview, you have several options to manage binding results of your data source which are described below.
Bidirectional Binding Mode#
Flexible TreeView imposes special conditions on the type and behavior of data source in bidirectional binding mode! Read this topic for details.
Hierarchy#
Flexible TreeView supports binding of both plain and hierarchical data sources.
To create a hierarchical data source, define two properties in the data object in the data source:
- An identifier of the object itself (e.g., Id)
- An identifier of the parent object (e.g., ParentId)
Then set the names of these properties in the KeyFieldName and ParentFieldName properties of the DataBinding object accordingly.
Example
Note
The ParentId property can take a null
value for the root objects in the hierarchy.
The data source usage example (using List<T> as the data source type):
class Order
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Number { get; set; }
}
List<Order> list = new List<Order>();
// create root object
Order order = new Order();
order.Id = 1;
order.ParentId = null; // NULL parent for all root objects
order.Number = "No.1";
list.Add(order);
// create child object
order = new Order();
order.Id = 2;
order.ParentId = 1; // specify parent identifier for the child object
order.Number = "No.1.1";
list.Add(order);
// define hierarchy member names
tree.DataBinding.KeyFieldName = "Id";
tree.DataBinding.ParentFieldName = "ParentId";
// attach data source to the treeview
tree.DataBinding.DataSource = list;
Class Order
Public Property Id As Integer
Public Property ParentId As System.Nullable(Of Integer)
Public Property Number As String
End Class
Dim list As New List(Of Order)()
' create root object
Dim order As New Order()
order.Id = 1
order.ParentId = Nothing ' NULL parent for all root objects
order.Number = "No.1"
list.Add(order)
' create child object
order = New Order()
order.Id = 2
order.ParentId = 1 ' specify parent identifier for the child object
order.Number = "No.1.1"
list.Add(order)
' define hierarchy member names
tree.DataBinding.KeyFieldName = "Id"
tree.DataBinding.ParentFieldName = "ParentId"
' attach data source to the treeview
tree.DataBinding.DataSource = list
DataBindingElementAttribute#
By default, Flexible TreeView automatically determines which fields of the data source to bind and how to bind them. If you have control over the structure of the binding data source type, you can use the DataBindingElementAttribute to fine-tune the data binding result and provide more specific instructions to the treeview on how you want the data object to be bound.
The DataBindingElementAttribute attribute can be applied to every data object property or field:
The DataBindingElementAttribute attribute properties are described below.
Visible#
By default, Flexible TreeView adds all public properties of the data source object to the treeview in form of columns and node controls. If you have access to the source code of the object class stored in the data source, you can use the DataBindingElementAttribute attribute to control data property visibility in the treeview by setting the Visible parameter.
Hide a data property from the treeview
The Sum property in the above example will not be processed by the Flexible TreeView parser, which is why such a column will not appear in the treeview.
DisplayName#
By default, Flexible TreeView generates a separate column for every field of the data object. You can customize the column caption created from the bound property by setting the custom caption in the DisplayName property of the DataBindingElementAttribute.
Example
Note
If the DisplayName property has the same value for several data object properties, all these properties will be displayed in one column with the caption indicated in DisplayName. In this case, the order they are displayed in the column will be identical to the sequence order of the properties in the data object class.
NodeControlType#
When binding a data source, Flexible TreeView decides on its own which node control type to create for each bound data property. If you have a custom-made node control and want to use it in the treeview, you can use the NodeControlType property to specify the node control type to create for the particular binding property.
Example
Note
The specified custom node control type should be derived from the existing node control type that is able to manage the bound property data type.
AutoSizeColumn#
By using the DataBindingElementAttribute attribute, you can also preset to have the width of a generated column calculated automatically based on the content in this column. To achieve this, you have to enable the AutoSizeColumn property.
Example
You can also explicitly specify the generated column width by using the ColumnWidth property as described below.
ColumnWidth#
By using the ColumnWidth property, you can explicitly specify the generated column width in pixels. This is useful when you know the width of the content shown in the particular column.
Example
FillFreeSpace#
By using the FillFreeSpace property, you can explicitly adjust the generated node control's FillFreeSpace property value to grab all the available column space for this node control. Read here about the FillFreeSpace node control property.
IsCheckState#
The IsCheckState property is intended to support interactive check state when the treeview is in bound mode.