Delphi LiveBindings: Format Calculated Fields Without Event Handlers
Use Delphi LiveBindings to declaratively format dataset values in UI controls, reducing manual event-handler code.
19 Aug 2025, 12:15 UTC

The Problem: Forgotten UI Updates
In a Delphi VCL or FMX form, keeping a label in sync with a dataset field often means writing code in OnDataChange, OnCalcFields, or button click handlers. If you add a second control that displays the same value, it is easy to update one and forget the other. As the form grows, these manual assignments become brittle and hard to audit.
Declarative Binding With LiveBindings
LiveBindings, available since Delphi XE2, lets you connect control properties to dataset fields without writing an event handler for every change. The binding engine evaluates the expression when the source data changes and updates the target control automatically.
Key Components
- TBindSourceDB: Exposes a dataset, such as
TClientDataSet, to the binding engine. - TBindExpression: Defines a formatting expression and maps the result to a control property.
- TLinkControlToField: Maps a single control property directly to a dataset field without custom formatting.
Worked Example: Formatted Total Label
This example runs in the Delphi IDE at design time. No special permissions are required beyond the ability to edit the project.
- Place a
TClientDataSetnamedcdsSalesand aTLabelnamedlblTotalon a VCL form. - Add a
TBindSourceDBnamedBindSourceDB1and set itsDataSetproperty tocdsSales. - Add a
TBindExpressionnamedBindExpression1. - Set
BindExpression1.SourceExpressionto'Total: ' + FloatToStr(Amount), whereAmountis a field incdsSales. - Set
BindExpression1.ControltolblTotalandBindExpression1.ControlExpressiontoText. - Set
BindExpression1.DataSourcetoBindSourceDB1.
When the dataset posts a new Amount value, the label should update to show the formatted string without any lblTotal.Text := ... code in your form unit.
Expected check: Open the form at runtime, edit a record in cdsSales, and post the change. The label text should reflect the new total.
Risk: A typo in SourceExpression may fail silently at runtime. The binding does not raise a Delphi exception; the control simply stops updating.
Trade-Offs and Limitations
LiveBindings evaluates expressions on each data change. For a form with a few labels, the cost is negligible. For grids or lists with thousands of rows, or bindings that update on every keystroke, the expression engine can add noticeable UI latency.
Debugging is also indirect. You cannot set a breakpoint inside a binding expression. Inspect the BindingsList component and the binding objects in the IDE watch window to confirm the source and target paths are correct.
Actionable Closing
Use LiveBindings for display-oriented forms where calculated or formatted values appear in multiple controls. Replace the binding with manual assignment in OnDataChange if you profile the application and find the expression evaluation is a bottleneck, or if you need to step through the logic with a debugger.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.