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;

你可能感兴趣的:(Delphi)