Bound Mode
- 5 minutes to read
The Bound Mode implies that the vertical grid control connects to a data source which is represented by TDataSet descendants. The TcxDBVerticalGrid control was developed to work in this mode, this control is represented by the TcxDBVerticalGrid class. Implementation of the Bound Mode is a simple task because the TcxDBVerticalGridDataController class was developed to work with this kind of data source. This class represents behavior that enables a developer to configure this grid control and retrieve data at design time or runtime in a few steps.
Below is an example on how to provide this mode at design time and runtime using the TDataSet descendant TADOTable class.
Binding to the Data Source at Design Time
To connect the TcxDBVerticalGrid control to the ADO table drag and drop the DataSource control onto a form (click the Data Access page tab in the Component Palette to get access to the control). Do the same with the ADOTable control residing in the ADO page of the Component Palette.
The Microsoft® Access CarsDB.mdb database will be used as the data source in this example. To map the TADOTable dataset to a data source select the ADOTable control on the form access the ADOTable control properties in the Object Inspector and give it the name “Cars”.
To establish a connection between the dataset and database set the TADOTable.ConnectionString property to string this will then be used to hold the information about the provider used and the database deployment path for this connection. To do this:
- Click on the button to the right of the property and the ConnectionString wizard will appear:
- Click the Use Connection String radio button and then click the Build… button to the right of the Use Connection String text field. The Data Link Properties window will appear. Click the Provider page tab and select Microsoft Jet 4.0 OLE DB Provider, then click the Next>> button or the Connection page tab to enter a path to the database name:
- Check you have entered the correct path to the database name and then click the Test Connection button on the Connection page. If the connection is successful a message box like this will appear:
Then click on the OK button in all open windows to save the changes and return to the Object Inspector.
Now select the database table (in this example Cars) and set the TADOTable.Active property to True to populate the dataset with data from the mapped data source:
After which you can create rows and connect them to the corresponding fields in the database table, there is also an option to do this in bulk:
- Double-click on the grid control or right-click on the grid control and select the Edit… item in the pop-up menu to invoke the Rows Collection Editor. Click the Create all items button in the Rows Collection Editor to create rows and populate them with data:
If you intend to have rows in a linear structure and no multi editor rows are required your vertical grid is ready to run:
Note
if you’d like to display and edit data in an appropriate fashion you need to bind a specific editor for the given type of data to every row. For details please refer to the Assigning Editors to Editor Rows topic.
Suppose you have a need to create rows step-by-step and connect every row to a corresponding field in a table. If the Rows Collection Editor window is opened click on the Add editor button to add just one editor row to the grid and give it a name of “ID”:
Using the TcxDBVerticalGridItemDataBinding.FieldName property bind this row to the corresponding ID field of the Cars table:
The ID row is bound to the table field and populated with its data:
Repeat the same for other rows.
Note
the multi editor row is just a container for items that are responsible for binding to the corresponding table fields. For details please refer to the Multi Editor Rows topic.
Binding to the Data Source at Runtime
To populate the TcxDBVerticalGrid control with data you’ll have to create rows and bind them to the corresponding fields of the database table as shown in this code snippet:
//...
with DBVerticalGrid.Add(TcxDBEditorRow) as TcxDBEditorRow do
begin
Properties.DataBinding.FieldName := 'ID';
Name := 'ID';
Caption := 'ID';
end;
Here is a full example on how to create rows that map to the database table structure and populate the vertical grid with a data:
//Delphi
unit BoundModeDemoMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, cxStyles, cxGraphics, cxEdit, cxControls, cxInplaceContainer, cxVGrid, cxDBVGrid, DB, ADODB, cxClasses;
type
TBoundModeDemoForm = class(TForm)
DBVerticalGrid: TcxDBVerticalGrid;
// don't forget to configure the ADOTable dataset as described above in this topic
DataSource: TDataSource;
Cars: TADOTable;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
BoundModeDemoForm: TBoundModeDemoForm;
implementation
{$R *.dfm}
procedure TBoundModeDemoForm.FormCreate(Sender: TObject);
begin
DBVerticalGrid.DataController.CreateAllItems;
end;
end.
Using the Object Inspector assign the TBoundModeDemoForm.FormCreate method to the BoundModeDemoForm OnCreate event to let the Delphi container call this method when the user launches the application.
The output when this code is running will be: