LiveBindings - TList<TMyObject> bound to TStringGrid
Asked Answered
A

1

11

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.
Akel answered 21/9, 2011 at 23:13 Comment(9)
Is the answer to this question of any help ? need-bidirectional-livebindings-between-a-control-and-an-objectSad
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.Akel
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.Sad
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.Akel
Have you seen this article: danieleteti.it/2011/08/30/… ?Failsafe
Yes, I have seen that article, but it does not answer this question.Akel
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!Trudi
You ever find a solution to this Robert? This seems like a fundamental feature of a data binding system.Domingodominguez
@Jim, No we did not figure it out. We finished writing our binding system it was easier.Akel
N
9

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; 
Notate answered 14/10, 2011 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.