Delphi XE2 之 FireMonkey 入门(31) - 数据绑定: 绑定数据库


一、全设计时操作:

先在窗体上放置控件:
DataSource1    : TDataSource;
ClientDataSet1 : TClientDataSet;
Label1         : TLabel;
Edit1          : TEdit;
Memo1          : TMemo;
ImageControl1  : TImageControl;
BindNavigator1 : TBindNavigator;

{在连接过程中, 会自动添加下面部件}
BindingsList1               : TBindingsList;
BindScopeDB1                : TBindScopeDB;

DBLinkLabel1SpeciesNo1      : TBindDBTextLink;
DBLinkEdit1Category1        : TBindDBEditLink;
DBLinkMemo1Notes1           : TBindDBMemoLink;
DBLinkImageControl1Graphic1 : TBindDBImageLink;


测试使用官方提供的测试数据 biolife.xml(或 biolife.cds), 其路径通常是: C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml

连接步骤:

1、置 DataSource1 的 DataSet 属性为 ClientDataSet1;

2、置 ClientDataSet 的 FileName 属性为 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';

3、将四个控件分别关联到 biolife.xml 中的字段:
   Label1       -> 右键 -> Link To DB Field... -> 选择 Species No (数字)
   Edit1        -> 右键 -> Link To DB Field... -> 选择 CateGory   (string)
   Memo1        -> 右键 -> Link To DB Field... -> 选择 Notes      (Text)
   ImageControl -> 右键 -> Link To DB Field... -> 选择 Graphic    (Graphics)

4、置 BindNavigator1 的 BindScope 属性为 BindScopeDB1;

5、置 ClientDataSet1 的 Active 属性为 True.


二、运行时连接:

先在窗体上放置控件(其它在运行时完成):
DataSource1    : TDataSource;
ClientDataSet1 : TClientDataSet;
Label1         : TLabel;
Edit1          : TEdit;
Memo1          : TMemo;
ImageControl1  : TImageControl;
BindNavigator1 : TBindNavigator;
BindingsList1  : TBindingsList;
BindScopeDB1   : TBindScopeDB;


与设计时功能相同的代码:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientDataSet1.FileName := 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';
  DataSource1.DataSet := ClientDataSet1;
  BindScopeDB1.DataSource := DataSource1;
  BindNavigator1.BindScope := BindScopeDB1;

  with TBindDBTextLink.Create(BindingsList1) do
  begin
    ControlComponent := Label1;
    DataSource := BindScopeDB1;
    FieldName := 'Species No';
  end;

  with TBindDBEditLink.Create(BindingsList1) do
  begin
    ControlComponent := Edit1;
    DataSource := BindScopeDB1;
    FieldName := 'Category';
  end;

  with TBindDBMemoLink.Create(BindingsList1) do
  begin
    ControlComponent := Memo1;
    DataSource := BindScopeDB1;
    FieldName := 'Notes';
  end;

  with TBindDBImageLink.Create(BindingsList1) do
  begin
    ControlComponent := ImageControl1;
    DataSource := BindScopeDB1;
    FieldName := 'Graphic';
  end;

  ClientDataSet1.Active := True;
end;


要绑定到 TStringGrid, 在设计时(在上面的基础上)只需: StringGrid1 -> Link to DB DataSource... -> BindScopeDB1

下面是运行时绑定到 TStringGrid 的代码:

uses Fmx.Bind.Editors, Data.Bind.DBLinks, Fmx.Bind.DBLinks;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientDataSet1.FileName := 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';
  DataSource1.DataSet := ClientDataSet1;
  BindScopeDB1.DataSource := DataSource1;
  BindNavigator1.BindScope := BindScopeDB1;
  ClientDataSet1.Active := True;

  with TBindDBGridLink.Create(BindingsList1) do //还可用 TBindGridLink
  begin
     GridControl := StringGrid1;
     DataSource := BindScopeDB1;
     Active := True;
  end;
end;

转载于:https://my.oschina.net/hermer/blog/319742

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值