Skip to main content
A newer version of this page is available.

Connecting to Data

  • 5 minutes to read

This topic explains how to connect the ExpressQuantumGrid to a dataset (which is a required step when working in bound mode). There will be a single level with a single View within the grid control.

Generally, the connection procedure can be divided into several steps:

1. Data Preparation

First, all necessary data objects (dataset and data source) are created and set up.

2. Grid Control Preparation

Then, a grid control with the appropriate data-aware View is created.

3. Data Connection

Finally, data is connected to the grid View.

Below, these steps are discussed in detail.

Data Preparation

The type of underlying data engine used by the grid control is not important. It can be BDE, ADO, ODBC, etc. The only requirement is that you should use the TDataSource component as the link between the dataset and the grid control.

We will discuss two methods of data preparation: BDE and ADO.

1. BDE

  • Place TTable and TDataSource components onto a form.

  • Setup the TTable component via the Object Inspector: Set its DatabaseName property to “DBDEMOS”, the TableName property to “biolife.db” and the Active property to True.

  • Setup the TDataSource component via the Object Inspector: Set its DataSet property to the TTable object (normally Table1).

2. ADO

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 either the DBDEMOS.mdb or BCDEMOS.mdb databases which are located in the Borland sample data files directory (assuming these files have been installed during a Delphi or C++ Builder installation). Click the [OK] button to apply changes and exit the Connection String Builder.

Click the [OK] button to apply 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.

  • Setup the TADOTable component via the Object Inspector: Set its Connection property to ADOConnection1, the TableName property to “country” and the Active property to True.

  • Setup the TDataSource component via the Object Inspector: Set its DataSet property to a TADOTable object (normally ADOTable1).

Grid Control Preparation

Drop the Grid control onto a form and set its Align property to alClient. The grid control already has a level and a DB Table View, so there is no need to create them.

Data Connection

After the data and grid control are prepared, you should connect them:

  • Set the DataSource property of the View’s data controller to a TDataSource object (normally DataSource1):

  • Now, the data is connected to the Grid control and it’s time to display the data. Right-click the View within the Structure Navigator to display the popup menu. Select the “Create All Columns” menu item.

Easy Steps to Connect

Whenever you have active datasets in a form that are accessible via the TDataSource components and the grid control has only one level which is associated with a data-aware View, all you have to do to connect the grid to a data source is right-click on the grid and choose the Link to <DataSource name> option from the dropdown menu as shown in the image below:

If there are several data sources available, then the “Link to DataSource“ menu item containing a submenu will be shown instead. This submenu will list the available data sources:

Linking the View to the data source in this manner will automatically connect the View to the selected data source and retrieve all the fields which will be displayed as View items. For a Banded Table View, the columns are placed within the first visible band, if any.

If the grid control has one or more levels, select the required View in the Structure Navigator and invoke the View’s context menu to link this View to the required data source.

In addition, you can open the IDE’s Fields Editor available for a dataset, table, query, or any other TDataSet descendant, and drag its fields directly to a data-aware Table View, Banded Table View, Card View, or Layout View to automatically create View items bound to these fields. The grid will automatically bind to a data source linked to the dataset (if there is no linked data source, it will be created and linked on the fly) before creating bound View items.

To connect your grid control to a dataset at runtime, implement the following code:

with cxGrid1DBTableView1.DataController do
  begin
    DataSource := DataSource1;
    CreateAllItems;
  end;

Note

The CreateAllItems method creates items within a View for each field in the linked dataset.

If you’ve done everything correctly, data will appear within a grid View:

1. BDE

2. ADO

See Also