2011-09-21 8 views
9

Tengo el siguiente conjunto de código de ejemplo, ¿cómo puedo vincular los elementos de lista Data al TStringGrid usando LiveBindings. Necesito actualizaciones bidireccionales para que cuando se cambie la columna de la cuadrícula pueda actualizar el TPerson subyacente.LiveBindings - TList <TMyObject> enlazado a TStringGrid

He visto un ejemplo de cómo hacer esto con un enlace basado en TDataset pero necesito hacer esto sin un 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. 
+0

en la respuesta a esta pregunta de alguna ayuda? [need-bidirectional-livebindings-between-a-control-and-an-object] (http://stackoverflow.com/questions/7478785/need-bidirectional-livebindings-between-a-control-and-an-object) –

+0

No ... Phil (quien hizo/contestó esa pregunta) y yo somos compañeros de trabajo tratando de resolver esto. Pero no han podido descifrar las expresiones necesarias para que funcione una red. –

+0

Ok, supongo que el framework FM carece de documentación en este momento. Como nota al margen, ¿cuál será la forma preferida de hacer esto vinculando, en código o escondido en el diseñador? Personalmente, odiaría esconder la lógica del código. –

Respuesta

8

Parte de la solución: De TList a TStringGrid es:

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; 
+0

+1 para poder averiguar cómo hacerlo de una manera. –

Cuestiones relacionadas