10

I have the following example set of code, how can I bind the Data list elements to the TStringGrid using LiveBindings. I need bi-directional updates so that when the column in the grid is changed it can update the underlying TPerson.

I have seen example of how to do this with a TDataset Based binding but I need to do this without a TDataset.

unit Unit15;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, System.Generics.Collections;

type
  TPerson = class(TObject)
  private
    FLastName: String;
    FFirstName: string;
  published
    property firstname : string read FFirstName write FFirstName;
    property Lastname : String read FLastName write FLastName;
  end;

  TForm15 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Data : TList<TPerson>;
  end;


var
  Form15: TForm15;



implementation

{$R *.dfm}

procedure TForm15.FormCreate(Sender: TObject);
var
 P : TPerson;
begin
  Data := TList<TPerson>.Create;
  P := TPerson.Create;
  P.firstname := 'John';
  P.Lastname := 'Doe';
  Data.Add(P);
  P := TPerson.Create;
  P.firstname := 'Jane';
  P.Lastname := 'Doe';
  Data.Add(P);
  // What can I add here or in the designer to link this to the TStringGrid.
end;

end.
Robert Love
  • 12,447
  • 2
  • 48
  • 80
  • Is the answer to this question of any help ? [need-bidirectional-livebindings-between-a-control-and-an-object](http://stackoverflow.com/questions/7478785/need-bidirectional-livebindings-between-a-control-and-an-object) – LU RD Sep 22 '11 at 06:16
  • Nope... Phil (Who asked/answered that question) and I are are coworkers trying to figure this all out. But't have not been able to figure out the expressions need to make a grid work. – Robert Love Sep 22 '11 at 07:05
  • Ok, I guess the FM framework lacks some documentation at the moment. As a sidenote, what will be the preferred way to do this linking, in code or hidden in the designer ? Personally I would hate to hide the logic from code. – LU RD Sep 22 '11 at 07:17
  • LiveBindings works with VCL and is not specific to FM. The Preferred way would be only determined once I can see how it ss done. But I personally like to see things in code. – Robert Love Sep 22 '11 at 07:59
  • Have you seen this article: http://www.danieleteti.it/2011/08/30/in-the-core-of-livebindings-expressions-of-rad-studio-xe2/ ? – Torbins Sep 22 '11 at 08:56
  • Yes, I have seen that article, but it does not answer this question. – Robert Love Sep 22 '11 at 13:41
  • 1
    I've tried for an hour and couldn't figure this out. Seems like something that should be very easy to do to but somehow it isn't. I hope someone comes up with a solution! – Birger Sep 23 '11 at 07:12
  • You ever find a solution to this Robert? This seems like a fundamental feature of a data binding system. – Jim McKeeth Oct 14 '11 at 06:50
  • 1
    @Jim, No we did not figure it out. We finished writing our binding system it was easier. – Robert Love Oct 14 '11 at 09:01

1 Answers1

8

Part of the solution: From TList to TStringGrid is:

procedure TForm15.FormCreate(Sender: TObject); 
var 
 P : TPerson; 
 bgl: TBindGridList;
 bs: TBindScope;
 colexpr: TColumnFormatExpressionItem;
 cellexpr: TExpressionItem;
begin 
  Data := TList<TPerson>.Create; 
  P := TPerson.Create; 
  P.firstname := 'John'; 
  P.Lastname := 'Doe'; 
  Data.Add(P); 
  P := TPerson.Create; 
  P.firstname := 'Jane'; 
  P.Lastname := 'Doe'; 
  Data.Add(P); 
  // What can I add here or in the designer to link this to the TStringGrid. 

  while StringGrid1.ColumnCount<2 do
    StringGrid1.AddObject(TStringColumn.Create(self));

  bs := TBindScope.Create(self);

  bgl := TBindGridList.Create(self);
  bgl.ControlComponent := StringGrid1;
  bgl.SourceComponent := bs;

  colexpr := bgl.ColumnExpressions.AddExpression;
  cellexpr := colexpr.FormatCellExpressions.AddExpression;
  cellexpr.ControlExpression := 'cells[0]';
  cellexpr.SourceExpression := 'current.firstname';

  colexpr := bgl.ColumnExpressions.AddExpression;
  cellexpr := colexpr.FormatCellExpressions.AddExpression;
  cellexpr.ControlExpression := 'cells[1]';
  cellexpr.SourceExpression := 'current.lastname';

  bs.DataObject := Data;
end; 
Arjen van der Spek
  • 2,630
  • 16
  • 20