Delphi LiveBindings-TList<;TMyObject>;绑定到TStringGrid
我有以下示例代码集,如何使用LiveBindings将Delphi LiveBindings-TList<;TMyObject>;绑定到TStringGrid,delphi,delphi-xe2,livebindings,Delphi,Delphi Xe2,Livebindings,我有以下示例代码集,如何使用LiveBindings将数据列表元素绑定到TStringGrid。我需要双向更新,这样当网格中的列发生更改时,它可以更新底层TPerson 我已经看到了如何使用基于TDataset的绑定执行此操作的示例,但我需要在没有TDataset的情况下执行此操作 unit Unit15; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Cl
数据
列表元素绑定到TStringGrid
。我需要双向更新,这样当网格中的列发生更改时,它可以更新底层TPerson
我已经看到了如何使用基于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.
单元单元15;
接口
使用
Winapi.Windows、Winapi.Messages、System.SysUtils、System.Variants、System.Classes、Vcl.Graphics、,
控件、窗体、对话框、网格、系统泛型集合;
类型
TPerson=类(TObject)
私有的
FLastName:String;
FFirstName:字符串;
出版
属性名:字符串读取FFirstName写入FFirstName;
属性Lastname:字符串读取FLastName写入FLastName;
结束;
TForm15=类(TForm)
StringGrid1:TStringGrid;
过程表单创建(发送方:ToObject);
私有的
{私有声明}
公众的
{公开声明}
资料来源:TList;
结束;
变量
表格15:TForm15;
实施
{$R*.dfm}
过程TForm15.FormCreate(发送方:TObject);
变量
P:TPerson;
开始
数据:=TList.Create;
P:=TPerson.Create;
P.firstname:=“约翰”;
P.姓氏:='Doe';
数据。添加(P);
P:=TPerson.Create;
P.firstname:=“简”;
P.姓氏:='Doe';
数据。添加(P);
//我可以在此处或设计器中添加什么来将其链接到TStringGrid。
结束;
结束。
解决方案的一部分:从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;
过程TForm15.FormCreate(发送方:TObject);
变量
P:TPerson;
bgl:TBindGridList;
bs:TBindScope;
colexpr:TColumnFormatExpressionItem;
cellexpr:TExpressionItem;
开始
数据:=TList.Create;
P:=TPerson.Create;
P.firstname:=“约翰”;
P.姓氏:='Doe';
数据。添加(P);
P:=TPerson.Create;
P.firstname:=“简”;
P.姓氏:='Doe';
数据。添加(P);
//我可以在此处或设计器中添加什么来将其链接到TStringGrid。
而StringGrid1.ColumnCount对这个问题的答案有帮助吗?不。。。菲尔(问了/回答了这个问题)和我是同事,正在努力解决这一切。但是,我们还没有弄清楚制作网格所需的表达式。好吧,我想FM框架目前缺少一些文档。作为旁注,在代码中或隐藏在设计器中进行链接的首选方式是什么?就我个人而言,我不想对代码隐藏逻辑。我已经试了一个小时,但还是弄不明白。这似乎是一件很容易做到的事情,但不知何故却不是。我希望有人能想出一个解决办法@吉姆,不,我们没弄明白。我们完成了装订系统的编写,这样更容易了。