LiveBindings help Delphi/C++ developers to connect source and control component with no lines of code. Connecting to the Datasource or TPrototypeBindSource is straightforward way to connect to your controls. How about binding the custom data collections created for your application with your control components? No Idea ? Don’t worry. This post can guide you to do the job smoothly. TAdapterBindsource which supports adapters to connect to different types of data.
AdapterBindSource sample shows using the TAdapterBindSource to create a bidirectional binding between a generic TObjectList of user defined objects and a grid.
You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Components used in the Sample App:
- TAdapterBindSource to provide custom data to a grid at run time.
- TDataGeneratorAdapter It is used to generate some prototype data for design time purposes. It is replaced at runtime with the user defined data. It is optional and is only used for design time.
The grid and the adapters are linked using LiveBindings.
Implementation details :
- 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.
Run Time : At run time, the TDataGeneratorAdapter is replaced with the user-defined data.
- 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 Data : 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. Check out the full article in the DocWiki about the LiveBindings.AdapterBindSource Sample.