2011-09-21 1 views
9

다음 코드 예제 세트를 사용하면 Data 목록 요소를 LiveBindings를 사용하여 TStringGrid에 바인딩 할 수 있습니다. 양방향 업데이트가 필요하므로 그리드의 열을 변경하면 기본 TPerson을 업데이트 할 수 있습니다.LiveBindings - TList <TMyObject> TStringGrid에 바인딩

TDataset 기반 바인딩을 사용하여이 작업을 수행하는 방법을 본 적이 있지만 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

어떤 도움이 질문에 대한 대답인가? [필요와 양방향 라이브 바인딩 - 컨트롤과 개체] (http://stackoverflow.com/questions/7478785/need-bidirectional-livebindings-between-a-control-and-an-object) –

+0

Nope ... Phil (누구에게 물어 보았습니까/그 질문에 답했습니다) 나는이 모든 것을 파악하려고 노력하는 동료입니다. 그러나식이 그리드 작업을해야하는지 파악할 수는 없었습니다. –

+0

좋아, 나는 FM 프레임 워크가 지금은 설명서가 부족한 것 같아요. 사이드 노트로,이 링크를 코드에서 수행하거나 디자이너에서 숨길 때 선호되는 방법은 무엇입니까? 개인적으로 나는 코드에서 논리를 숨기기를 싫어한다. –

답변

8

부 : 사실상 TList에서 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 방법을 이해하는 방법에 대한 +1. –

관련 문제