Skip to content

Instantly share code, notes, and snippets.

@magnomp
Created November 24, 2011 19:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save magnomp/1392158 to your computer and use it in GitHub Desktop.
Save magnomp/1392158 to your computer and use it in GitHub Desktop.
Helper class for defining live bindings for a TStringGrid
{ Created it because I found that defining the columns at design time isn't much productive.
Usage:
Consider CustomersBindingScope is a TBindScope and CustomersGrid is a TStringGrid, and
CustomerBindingScope.DataObject points to a TList<TCustomer> and TCustomer is declared as
follow:
type
TCustomer = class
public
property Name: String read FName write FName;
property Age: Integer read FAge write FAge;
end;
with StringGridBinder(CustomersBindingScope, CustomersGrid) do
begin
Column('Customer Name', 'current.Name');
Column('Customer Age', 'current.Age');
end;
Note: In order for live bindings to work, your VCL project must include the unit Vcl.Bind.Editors
}
unit Common.Bindings;
interface
uses
Data.Bind.Components, Vcl.Grids, Generics.Collections;
type
{ Currently, this class didn't even need to exist, but I created it to be
a nice place to expose properties of the column }
TColumnBinder = class
public
constructor Create(const ColExprItem: TColumnFormatExpressionItem;
const Caption, Expr: String; const ColIndex: Integer);
end;
IStringGridBinder = interface
['{5371DFBD-1DEA-4A74-B5D3-73673330808C}']
function Column(const Caption, Expr: String): TColumnBinder;
end;
TStringGridBinder = class(TInterfacedObject, IStringGridBinder)
private
FBg: TBindGridList;
FColCount: Integer;
FToFree: TList<TObject>;
public
constructor Create(const Scope: TBindScope; const Grid: TStringGrid);
function Column(const Caption, Expr: String): TColumnBinder;
destructor Destroy; override;
end;
function StringGridBinder(const Scope: TBindScope; const Grid: TStringGrid): IStringGridBinder;
implementation
uses
SysUtils;
function StringGridBinder(const Scope: TBindScope; const Grid: TStringGrid): IStringGridBinder;
begin
Result := TStringGridBinder.Create(Scope, Grid);
end;
{ TStringGridBinder }
constructor TStringGridBinder.Create(const Scope: TBindScope;
const Grid: TStringGrid);
begin
FToFree := TList<TObject>.Create;
FBg := TBindGridList.Create(Grid);
FBg.ControlComponent := Grid;
FBg.SourceComponent := Scope;
end;
destructor TStringGridBinder.Destroy;
var
O: TObject;
begin
for O in FToFree do
O.Free;
FToFree.Free;
inherited;
end;
function TStringGridBinder.Column(const Caption, Expr: String): TColumnBinder;
begin
Result := TColumnBinder.Create(FBg.ColumnExpressions.AddExpression, Caption, Expr, FColCount);
FToFree.Add(Result);
Inc(FColCount);
end;
{ TColumnBinder }
constructor TColumnBinder.Create(const ColExprItem: TColumnFormatExpressionItem;
const Caption, Expr: String; const ColIndex: Integer);
begin
with ColExprItem.FormatCellExpressions.AddExpression do
begin
SourceExpression := Expr;
ControlExpression := 'cells[' + IntToStr(ColIndex) + ']';
end;
with ColExprItem.FormatColumnExpressions.AddExpression do
begin
SourceExpression := QuotedStr(Caption);
ControlExpression := 'cells[' + IntToStr(ColIndex) + ',0]';
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment