Connecting to Data
- 4 minutes to read
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:
1. Data Preparation
First, all data objects necessary (dataset and data source) are created and set up.
2. TreeList Control Preparation
A TreeList control with an appropriate view is created.
3. Data Connection
Finally, data is connected to the control.
Now let’s discuss these steps in detail:
Data Preparation
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.
1. BDE
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.
- Set up the TDataSource component via the Object Inspector: Set its DataSet property to a TTable class instance (normally Table1).
2. ADO
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.
- Set up the TADOTable component via the Object Inspector: Set its Connection property to ADOConnection1, the TableName property to “DEPARTMENTS” and the Active property to True.
- Set up the TDataSource component via the Object Inspector: Set its DataSet property to a TADOTable class instance (normally ADOTable1).
TreeList Control Preparation
Drop the TcxDBTreeList control on a form and set its Align property to alClient. Only this TreeList control can be connected to a dataset.
Data Connection
The next step is to connect the TreeList control to the data prepared:
- Set the DataSource property of the data controller to a TDataSource class instance (normally DataSource1):
- Set the KeyField property to the key field in a self-referenced dataset:
- Set the ParentField property to the parent field in the self-referenced dataset:
- Now the data is connected to the TreeList control. To display the data, call the Component Editor, go to the Columns tab and press the Create all fields button. Alternatively, you can right-click the TreeList control and select the Create All Columns item from the context menu.
Easy Steps to Connect
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;
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: