LiveBindings in VCL Part 1: LiveBinding DataSets

This is the first post of a series looking at using VCL and LiveBindings. This posts will cover using LiveBindings with TDataSet.
Post two will look at using Objects, and Post 3 will look at dynamic creating bindings at runtime.

Background

When programming, something most applications have is a user interface. A key duty of the user interface is to show the values and data inside the application and allow interaction with it.

Delphi and C++ Builder excelled from the beginning with TDataSet and TDataSource enabling developers to rapidly prototype database applications, linking data to controls via a couple of properties and making them fully data aware and interact-able. This was, and still is awesome. This approach however has some drawbacks.

  1. You always need data aware versions of your controls
  2. It only works with TDataSet descendants (i.e. not with TObject)

Live Bindings was introduced in DelphiC++ Builder , RAD Studio XE3 primarily as a way to link FireMonkey Controls (that don’t use the TDataSource model) to data, but also allow any object to be bound to and shown visually.

LiveBindings in Delphi & C++ Builder VCL applications

While LiveBlindings is at the core of fast FireMonkey prototyping, the VCL still has a rich heritage of TDataSource components that enable prototyping the traditional way using TDBGrid, TDBEdit, TDBLabel etc. So is LiveBindings useful or needed in VCL?

Beyond TDBxxxx  the VCL also has a rich set of inbuilt and 3rd party controls (e.g. TListView) that are not data aware in the traditional approach. LiveBindings is however a doorway to using these controls alongside the traditional data aware controls.

LiveBindings is a powerful way to add in new functionality to existing applications or mix in functionality that you wouldn’t have thought possible in the traditional VCL model as we are about to see.

Making VCL TListView data aware!

Taking the example of a database aware VCL application we are going to add TListView as an alternative source for the data.

The start point for this application is a traditional VCL application with a TDataSource and TDataSet, (I’m using a TClientDataSet and biolife sample XML data as I can quickly load this in from the samples folder).

The DataSource.DataSet property is set to the ClientDataSet and the DataSource is used to link 3 data aware controls (2 x TDBEdit and 1 x TDBImage) to the data.

VCL Fish Facts

At this point, rather than using a TDBGrid to create a master list on the left to navigate the data, I will show how we can use a TListView a non data aware control to Group the data in a list.

Firstly, we need the data in order in the DataSet so they group correctly. This is done with the ClientDataSet by setting the IndexFieldNames property. In this example it is set to “Category;Common_Name” to order the data by Category and name.

Now its time to add a TListView component and set the following properties to show the data in groups and columns

  • GroupView = True
  • ViewStyle = vsReport

And add some columns, I added Common Name and Length CM, but you can put anything as the titles. (we are going to use Category as the group so no need to have a column for that)

ListView Groups

Finally, its time to get the data added. Traditionally with data aware controls we would use would run via a TDataSource, but we will use LiveBindings. Right click on the TForm and choose Bind Visually (or choose View > LiveBindings Designer)

You will now see components on the form represented in the LiveBindings Designer. Select “Synch” on the ListView by clicking and holding the mouse button down, and move the mouse over the * on the DataSet.  This will create a binding that keeps the record cursor inline with the data on the screen.

LiveBindings for TListView

Next, we can add the fields to the ListView properties.

  • Item.Caption = Common_Name
  • Item.SubItems[0] = Length (cm)
  • ItemHeader.Header = Category

You should now see the data as in the image above, and at run time, selecting the items in the ListView will automatically update the cursor in the DataSet.

 

5 thoughts on “LiveBindings in VCL Part 1: LiveBinding DataSets”

  1. Fantastic quick demo. After its initial introduction live bindings received multiple updates so it is good to have a more current set of videos that reflects the current status of it.

    It is a very underused feature that needs more attention.

  2. Thank you. It will be nice to have other examples as lookupcombobox, combo box and why not all TDBxxxx livebindings counterparts 🙂

Leave a Reply

Your email address will not be published.