Connecting to Data in Server Mode
- 5 minutes to read
This topic explains how to bind the ExpressQuantumGrid control to a database in server mode. We will create a simple grid layout with one grid level and one Table View. To see how to bind the ExpressQuantumGrid control to a Microsoft SQL Server database, run the ServerModeFireDACDemo, ServerModeFireDACQueryDemo, ServerModeADODemo, ServerModeADOQueryDemo, ServerModeDBXDemo, or ServerModeDBXQueryDemo demo shipped with the ExpressQuantumGrid Suite.
Generally, the connection procedure includes the following steps.
1. Data Preparation
Create and set up all necessary data access objects (connection and data source components).
2. Grid Control Preparation
3. Data Connection
Finally, connect the grid View to data.
Below, these steps are described in detail.
Server mode requires an active database connection and a corresponding data source component to access data in a specific database.
To obtain data from a specific database table or view, use either of the following:
A FireDAC connection and the DevExpress table-based data source designed for this connection. To use this connection type, drop the TFDConnection/TADConnection and TdxServerModeFireDACDataSource components onto a form;
A dbExpress connection and the DevExpress table-based data source designed for this connection. To use this connection type, drop the TSQLConnection and TdxServerModeDBXDataSource components onto a form;
To query one or more database tables or views using SQL statements, use either of the following:
A FireDAC connection and the DevExpress query-based data source designed for this connection. To use this connection type, drop the TFDConnection/TADConnection and TdxServerModeFireDACQueryDataSource components onto a form;
A dbExpress connection and the DevExpress query-based data source designed for this connection. To use this connection type, drop the TSQLConnection and TdxServerModeDBXQueryDataSource components onto a form;
An ADO connection and the DevExpress query-based data source designed for this connection. To use this connection type, drop the TADOConnection and TdxServerModeADOQueryDataSource components onto a form.
To establish a connection to a database, provide your connection component with required settings and set its Connected property to True. The current server mode implementation provides support for Microsoft SQL Server, Microsoft Access, MySQL, Firebird, InterBase, Oracle, Advantage, PostgreSQL, and SQLite databases. So, you need to target one of these databases when providing connection settings.
Once a database is successfully connected, accomplish the following to set the data source component to access data via the established connection object.
Link the server mode data source to the connection object via the data source’s Connection property;
Select the SQL query adapter that corresponds to the target database type, via the SQLAdapter property.
Optional: With the TdxServerModeFireDACDataSource, TdxServerModeDBXDataSource, or TdxServerModeDBXQueryDataSource data source, you can specify the schema name for Microsoft SQL Server databases, using the Options.SchemaName property. If not specified, the default schema will be used.
For a table-based server mode data source, select the target database table or view via the TableName property. For a query-based server mode data source, specify the SQL statement for the query via the SQL property.
Important: Server mode requires that the table have at least one key field – a field whose values uniquely identify records. You must select one or more key fields via the KeyFieldNames property. Use a semicolon to delimit multiple field names. Note that in table-based server mode data sources, if you leave this property unspecified, the data source component uses the field or fields that define the table’s primary key (a simple or compound primary key, respectively). In this instance, table fields included into the primary key are displayed in bold in the KeyFieldNames property’s drop-down list. If the primary key does not exist (no fields in the list are displayed in bold), you must define a key by specifying one or more field names via this property.
Switch the Active property to True to let the data source obtain fields and a small set of data.
Below are examples on how to set the settings of table-based server mode data sources.
The data source component retrieves all the fields contained in the target source (the table/view or the query’s result set) by default. You can customize the field list via the IDE’s Fields Editor accessible via the component’s context menu.
Grid Control Preparation
Drop the ExpressQuantumGrid control (the TcxGrid component) onto a form. Initially, the grid control has a grid level with assigned DB Table View. For the server mode, you need to use the Server Mode Table View or Server Mode Banded Table View as a data-aware View. For demonstration purposes in this topic, we’ll use the Server Mode Table View.
Click “Delete View” in the View’s context menu to remove the automatically created DB Table View.
Then, invoke the context menu for the grid level, click “Create View” | “Server Mode Table” to create the required View.
Now the View is ready to be connected to data.
After the data access objects and grid control are prepared, you can link them using any of the following methods.
- Invoke the context menu for the server mode View and click “Link to <DataSource name>” item. If you have multiple server mode data sources on a form, the item will show a sub-menu containing these data sources to allow you to choose one. Once a data source is linked, the View automatically creates bound columns for all fields available within the data source;
- Click the server mode View to display its properties in the Object Inspector and select the data source within the DataController.DataSource property list. Then, you can create columns bound to the data source’s fields. To accomplish this, invoke the View’s context menu and click “Create All Columns”. Alternatively, you can use Component Editor capabilities for creating columns.