LiveBindings.AdapterBindSource Sample
This sample project demonstrates the use of a TAdapterBindSource.
Contents
Location
You can find the AdapterBindSource sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to Object Pascal\LiveBindings\AdapterBindSource
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
This sample demonstrates the use of TAdapterBindSource and TDataGeneratorAdapter.
- TAdapterBindSource to provide custom data to a grid at run time.
- TDataGeneratorAdapter to provide random data to a grid at design time.
The grid and the adapters are linked using LiveBindings.
The sample also uses LiveBindings to bind a TBindNavigator with a TGrid.
How to Use the Sample
- Navigate to the location above and open AdapterBindSourceSample.dproj.
- Press F9 or choose Run > Run.
- When you run the application, the TGrid is populated with a user-defined list.
- Use the TBindNavigator to navigate through the rows and to add/delete items at run time.
- Note: The modifications to the fields remain only at run time.
- Use the TBindNavigator to navigate through the rows and to add/delete items at run time.
- Go to the JSON tab to see a JSON representation of the data.
- Any modification over the data in the Grid or JSON tab applies to the other tab at run time.
Files
File | Contains |
---|---|
AdapterBindSourceSample.dproj |
The project itself. |
ABSMain.fmx / pas |
The main form and main unit. |
CollectionObjects.pas |
The unit with the definition of the custom class TPerson. |
ABSMain.iPhone4in.fmx / ABSMain.LgXhdpiPh.fmx |
View forms for iPhone and Android. |
Implementation
At Design Time
The Adapter property of the TAdapterBindSource is filled at design time with the TDataGeneratorAdapter to generate some prototype data.
Three FieldDefs items are added to the TDataGeneratorAdapter using the Fields Editor.
The sample uses LiveBindings to create the binding between the adapter and the grid.
At run time, the TDataGeneratorAdapter is replaced with the user-defined data.
At Run Time
CollectionObjects.pas contains a custom class, TPerson, that contains three properties: FirstName, LastName and Age.
The OnCreateAdapter event of the TAdapterBindSource is used to initialize a user defined list. The list is a TObjectList that contains individual TPerson objects.
After creating the list, the collection is populated with hard-coded data provided in ABSMain.pas.
An adapter ( TBindSourceAdapter) provides the data to the grid. It uses TListBindSourceAdapter to implement the adapter for the user-defined list of objects.
LiveBindings creates the binding between the adapter and the grid.
JSON Tab
The sample uses the OnChange event from the TTabControl to populate the TMemo from the JSON tab with the JSON representation of the data from the grid.
Making changes on the Grid data, updates the data from the JSON tab, and vice versa.