Skip to content

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

class Order
{
    public int Id { get; set; }
    public int? ParentId { get; set; }
    public string Number { get; set; }
}

treeview.DataBinding.KeyFieldName = "Id";
treeview.DataBinding.ParentFieldName = "ParentId";
Class Order
    Public Property Id As Integer
    Public Property ParentId As System.Nullable(Of Integer)
    Public Property Number As String
End Class

treeview.DataBinding.KeyFieldName = "Id"
treeview.DataBinding.ParentFieldName = "ParentId"

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:

class DataObject
{
  [DataBindingElement(...)]
  public decimal SomeProperty { get; set; }
}
Class DataObject
    <DataBindingElement(...)>
    Public Property SomeProperty As Decimal
End Class

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

class Order
{
    public int Id { get; set; }

    public int? ParentId { get; set; }

    public string Number { get; set; }

    [DataBindingElement(Visible = false)]
    public decimal Sum { get; set; }
}
Class Order  
    Public Property Id As Integer  

    Public Property ParentId As System.Nullable(Of Integer)  

    Public Property Number As String  

    <DataBindingElement(Visible:=False)>  
    Public Property Sum As Decimal  
End Class

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

class Order
{
    [DataBindingElement(DisplayName = "Custom caption")]
    public decimal Sum { get; set; }
}
Class Order
    <DataBindingElement(DisplayName := "Custom caption")>
    Public Property Sum As Decimal
End Class

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

class Order
{
    [DataBindingElement(NodeControlType = typeof(MyNodeNumeric))]
    public decimal Sum { get; set; }
}
Class Order
    <DataBindingElement(NodeControlType := GetType(MyNodeNumeric))>
    Public Property Sum As Decimal
End Class

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

class Order
{
    [DataBindingElement(AutoSizeColumn = true)]
    public decimal Sum { get; set; }
}
Class Order
    <DataBindingElement(AutoSizeColumn := True)>
    Public Property Sum As Decimal
End Class

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

class Order
{
    [DataBindingElement(ColumnWidth = 200)]
    public decimal Sum { get; set; }
}
Class Order
    <DataBindingElement(ColumnWidth := 200)>
    Public Property Sum As Decimal
End Class

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.