This is the start of an in-depth series on the new LiveBindings technology found in Delphi XE2. At its most basic level LiveBindings is the mechanism provided for FireMonkey applications to display and edit data from a database on screen, a job that in the VCL is traditionally done with data-aware controls. FireMonkey however has no data-aware controls, instead LiveBindings allow you to bind database fields to standard controls.
There is much more to LiveBindings, but for today we’ll look at this most simplest of use cases, displaying and editing a database table in a FireMonkey application.
Firstly we need to create a new FireMonkey HD Application by selecting
- FireMonkey HD Application
Now we need a database for our application to access. Drop a TClientDataSet (or any other TDataSet descendent) and a TDataSource on the form just as you would in a VCL application.
Connect the TDataSource to the TClientDataSet by it’s DataSet property, exactly as you do with a VCL application.
If you used a TClientDataSet you’ll need to specify an XML data file for it to load. I’m using the biolife.xml file from the Delphi XE2 samples directory.
Now we start placing components on the form. Start with a TStringGrid.
Note that it isn’t TDBGrid or any other TDB style of component.
Next drop onto the form one of each of these standard controls
Your form should now resemble this.
Now we need to start binding these standard controls to the database fields.
Drop a TBindingsList anywhere onto your form.
Follow it with a TBindScopeDB, also anywhere on your form.
Now we link the binding engine to the database engine by the DataSource property of the TBindScopeDB.
With the TLabel selected, in the Object Inspector select
- Dropdown arrow
- Link to DB Field
Select the field you wish to display in this label control. In this example I’m displaying the Common_Name field.
Repeat this process for the TEdit control. In the Object Inspector select
- Dropdown arrow
- Link to DB Field,
and select the field for the TEdit to display/edit.
A TMemo is bound the same way, selecting “Link to DB Field” from the LiveBindings property in the Object inspector
and then selecting a BLOB field from the field list.
In the biolife.xml file this is the Notes field.
Binding a TStringGrid is slightly different. In the object inspector
- Dropdown arrow
- Link to DB DataSource
Rather than selecting an individual field for a grid we select the parent TBindScopeDB.
Lastly the TClientDataSet needs to be opened when the application runs.
You can now run the application and edit the TClientDataSet data from within the standard non data-aware controls.
Nice article. You might also be interested in the “White Paper” document on LiveBindings I created for our local BeDelphi conference a month ago. Maybe you will find more info in there 🙂 You should be able to find my slides and the PDF version right here http://bit.ly/LiveBindings
Good detailed post Lachlan. For anyone that might be wondering, Live Binding *is* available for VCL as well. (Your 2nd sentence may be misunderstood to imply otherwise.)
cheers – Ian
LiveBindings for VCL is coming in a future post very soon.
Simple and clear to understand explanation.
Another Fish Facts tutorial –
Well nice but have a problem with FetchRecord property on IBCQuery. All records fetching when use LiveBindings in XE2/XE3