HeaderPane
Main
Hide
Share Link
Print

Data Binding

The control can show the rows of a data source as its items. The SftTree.DataSource property defines the data source: a list (IList, such as an array, a List or a BindingList), a list source (IListSource, such as a DataTable or DataSet) or a BindingSource. When the data source holds several lists, such as the tables of a DataSet, the SftTree.DataMember property names the list to use.

Data Binding

Columns

Each row becomes an item. The ColumnClass.DataMember property of each column names the property (or data column) of the rows shown in the column's cells. ColumnClass.DataFormat defines a .NET format string for the values, such as "d", "C2" or "N0", and SftTree.DataFormatInfo the format provider, which is the current culture by default.

Hierarchy

Without further settings, all rows are top-level items. A hierarchy is defined in one of two ways:

  • By keys: SftTree.DataKeyMember names the property that identifies a row and SftTree.DataParentMember the property that holds the key of the row's parent row. A row whose parent value equals another row's key becomes that row's child item. Rows without a matching parent are top-level items, and so are rows whose parent keys form a cycle.
  • By child lists: SftTree.DataChildrenMember names a property of a row that returns its child rows (IEnumerable). Each child row becomes a child item, and its own children are found through the same property.

Changes of the Data Source

The control follows the changes a data source reports (IBindingList, such as a BindingList, a DataView or a BindingSource): rows that are added, removed or changed add, remove or update their items, and a new list rebuilds all items. Expanded items stay expanded and the selected item stays selected. The changes of child lists (SftTree.DataChildrenMember) are shown when the data source reports a change of the row that owns them.

A data source that is still initializing, such as a BindingSource set up in InitializeComponent, is used once its initialization has ended.

Selection and Current Row

The selected item and the current row of the data source (the Position of its CurrencyManager) follow each other: selecting an item makes its row the current row, and moving the current row, for example with a BindingNavigator, selects its item, expanding its parent items and scrolling it into view.

ItemClass.DataItem returns the row an item was created from, and SftTree.FindItem returns the item created from a row.

Customizing Items

The SftTree.ItemDataBound event occurs after an item has been created or updated from its row (ItemDataBoundEventArgs.Item, ItemDataBoundEventArgs.DataItem). It is the place to set images, check boxes, colors and fonts of the item and its cells, or to change the text of its cells.

Restrictions

While a data source is set, the items belong to it: the application cannot add, remove or sort items (the data source can be sorted instead), and changes made to cells are not written back to the data source. Setting SftTree.DataSource to null/Nothing removes the items created from it.

Example

C#

DataTable table = new DataTable();
table.Columns.Add("Id", typeof(int));
table.Columns.Add("ParentId", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Population", typeof(int));
table.Rows.Add(1, DBNull.Value, "Europe", DBNull.Value);
table.Rows.Add(2, 1, "France", 68400000);
table.Rows.Add(3, 1, "Spain", 48600000);
table.Rows.Add(4, DBNull.Value, "Asia", DBNull.Value);
table.Rows.Add(5, 4, "Japan", 123800000);

sftTree1.Columns.Count = 2;
sftTree1.Columns[0].DataMember = "Name";
sftTree1.Columns[1].DataMember = "Population";
sftTree1.Columns[1].DataFormat = "N0";
sftTree1.DataKeyMember = "Id";
sftTree1.DataParentMember = "ParentId";
sftTree1.DataSource = table;

VB

Dim table As New DataTable()
table.Columns.Add("Id", GetType(Integer))
table.Columns.Add("ParentId", GetType(Integer))
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Population", GetType(Integer))
table.Rows.Add(1, DBNull.Value, "Europe", DBNull.Value)
table.Rows.Add(2, 1, "France", 68400000)
table.Rows.Add(3, 1, "Spain", 48600000)
table.Rows.Add(4, DBNull.Value, "Asia", DBNull.Value)
table.Rows.Add(5, 4, "Japan", 123800000)

SftTree1.Columns.Count = 2
SftTree1.Columns(0).DataMember = "Name"
SftTree1.Columns(1).DataMember = "Population"
SftTree1.Columns(1).DataFormat = "N0"
SftTree1.DataKeyMember = "Id"
SftTree1.DataParentMember = "ParentId"
SftTree1.DataSource = table