2011-09-21 14 views
9

Ho il seguente esempio di codice, come posso associare gli elementi dell'elenco Data allo TStringGrid utilizzando LiveBindings. Ho bisogno di aggiornamenti bidirezionali in modo che quando la colonna nella griglia viene modificata, è possibile aggiornare il sottostante TPerson.LiveBindings - TList <TMyObject> associato a TStringGrid

Ho visto un esempio di come eseguire questa operazione con un binding basato su TDataset ma è necessario farlo senza 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

È la risposta a questa domanda di aiuto? [Bisogno-bidirezionali-livebindings-tra-a-control-e-un-oggetto] (http://stackoverflow.com/questions/7478785/need-bidirectional-livebindings-between-a-control-and-an-object) –

+0

No ... Phil (Chi ha chiesto/risponde a questa domanda) e io sono colleghi che cercano di capire tutto questo. Ma non sono stato in grado di capire le espressioni necessarie per far funzionare una griglia. –

+0

Ok, immagino che al momento il framework FM manchi di documentazione. Come sidenote, quale sarà il modo preferito per fare questo collegamento, in codice o nascosto nel designer? Personalmente odio nascondere la logica del codice. –

risposta

8

Parte della soluzione: Da TList a TStringGrid è:

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 per essere in grado di capire come farlo andare in un modo. –

Problemi correlati