2011-09-21 9 views
9

मेरे पास कोड का निम्न उदाहरण सेट है, मैं Data सूची तत्वों को TStringGrid पर लाइवबिंडिंग का उपयोग करके कैसे बाध्य कर सकता हूं। मुझे द्वि-दिशात्मक अपडेट की आवश्यकता है ताकि जब ग्रिड में कॉलम बदल दिया जाए तो यह अंतर्निहित TPerson अपडेट कर सकता है।लाइव बाइंडिंग्स - 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

किसी भी मदद के इस सवाल का जवाब है? [आवश्यकता द्विदिश-livebindings-बीच एक नियंत्रण और एक-वस्तु] (http://stackoverflow.com/questions/7478785/need-bidirectional-livebindings-between-a-control-and-an-object) –

+0

नहीं ... फिल (किसने पूछा/उत्तर दिया) और मैं सहकर्मी हैं जो इसे सब कुछ समझने की कोशिश कर रहे हैं। लेकिन अभिव्यक्तियों को समझने में सक्षम नहीं है कि ग्रिड काम करने की आवश्यकता है। –

+0

ठीक है, मुझे लगता है कि इस समय एफएम ढांचे में कुछ दस्तावेज नहीं हैं। एक sidenote के रूप में, इस लिंकिंग, कोड में या डिजाइनर में छिपाने के लिए पसंदीदा तरीका क्या होगा? व्यक्तिगत रूप से मैं कोड से तर्क छिपाने से नफरत करता हूं। –

उत्तर

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 यह पता लगाने में सक्षम होने के लिए कि इसे कैसे एक तरफ जाना है। –

संबंधित मुद्दे