vcl-170065-expressquantumtreelist-concepts-connecting-to-data.md
This topic explains how to connect ExpressQuantumTreeList to a dataset.
A data-aware TreeList control is able to connect to any VCL dataset but only self-referenced datasets are suitable for ExpressQuantumTreeList. In other cases, the control will not be able to build a tree.
Generally, the connection procedure can be divided into several steps:
First, all data objects necessary (dataset and data source) are created and set up.
A TreeList control with an appropriate view is created.
Finally, data is connected to the control.
Now let’s discuss these steps in detail:
The type of the underlying data engine used by the TreeList control is not important. It can be BDE, ADO, ODBC or whatever. The only requirement is that you should use the TDataSource component as a link between a self-referenced dataset and the control.
We will discuss two methods of data preparation: BDE and ADO.
Place TTable and TDataSource components on a form (or DataModule).
Set up the TTable component via the Object Inspector: Set its DatabaseName property to the catalog where you have installed the demo Data for the ExpressQuantumTreeList Suite, the TableName property to “departments.db” and the Active property to True.
Place TADOConnection, TADOTable and TDataSource components on a form.
Set up the TADOConnection component via the Object Inspector: Use the Connection String Wizard to form a connection string:
This is the Connection String Wizard. Press the [Build…] button to continue:
The following dialog will appear. Select the Microsoft Jet 4.0 OLE DB Provider data provider and click the Connection tab.
In the Connection tab, click the […] button next to the database name textbox. Navigate to the DepartmentsDB.mdb database. This file is shipped with the ExpressQuantumTreeList Library. Click the [OK] button to apply the changes and exit the Connection String Builder.
Click the [OK] button to apply the changes and exit the Connection String Wizard.
Set the LoginPrompt property to False to deactivate the database login dialog:
Now the ADO Connection is ready to use.
Drop the TcxDBTreeList control on a form and set its Align property to alClient. Only this TreeList control can be connected to a dataset.
The next step is to connect the TreeList control to the data prepared:
Whenever you have active datasets in a form that are accessible via the TDataSource components, all you have to do to connect the TcxDBTreeList control to a data source is invoke the tree list’s context menu and select the required data source via the “Link to DataSource” item. This will automatically create columns bound to dataset fields.
To connect your data-aware TreeList control to a dataset at runtime, implement the following code:
with cxDBTreeList1.DataController do
begin
DataSource := DataSource1;
CreateAllItems;
end;
cxDBTreeList1->DataController->DataSource = DataSource1;
cxDBTreeList1->DataController->CreateAllItems();
Note
The CreateAllItems method creates columns within a data-aware tree list for each field in the linked dataset.
If you’ve done everything correctly, data will appear within the control: