一、全设计时操作:

先在窗体上放置控件:


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

连接步骤:


、置 DataSource1 的 DataSet 属性为 ClientDataSet1;

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

、将四个控件分别关联到 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) 、置 BindNavigator1 的 BindScope 属性为 BindScopeDB1; 、置 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 XE2 之 FireMonkey 入门(31) - 数据绑定: 绑定数据库的更多相关文章

  1. Delphi XE2 之 FireMonkey 入门(30) - 数据绑定: TBindingsList: TBindExpression 的 OnAssigningValue 事件

    Delphi XE2 之 FireMonkey 入门(30) - 数据绑定: TBindingsList: TBindExpression 的 OnAssigningValue 事件 表达式中的函数有 ...

  2. Delphi XE2 之 FireMonkey 入门(29) - 数据绑定: TBindingsList: 表达式的 Evaluate() 方法

    Delphi XE2 之 FireMonkey 入门(29) - 数据绑定: TBindingsList: 表达式的 Evaluate() 方法 TBindingsList 中可能不止一个表达式, 通 ...

  3. Delphi XE2 之 FireMonkey 入门(27) - 数据绑定: TBindingsList: TBindScope

    Delphi XE2 之 FireMonkey 入门(27) - 数据绑定: TBindingsList: TBindScope 如果在编写表达式时, 如果能够随意指认需要的控件就好了(通过 Owne ...

  4. Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems

    Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems 如果要给一对 "源控件" 和 " ...

  5. Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数

    Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数 绑定表达式中可以有简单的运算和字符串连接, 但字符串需放在双引号 ...

  6. Delphi XE2 之 FireMonkey 入门(32) - 数据绑定: TBindingsList: TBindList、TBindPosition [未完成...]

    Delphi XE2 之 FireMonkey 入门(32) - 数据绑定: TBindingsList: TBindList.TBindPosition [未完成...] //待补...

  7. Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText()、CheckedState()

    Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText().CheckedState() 示例构想: 用 ...

  8. Delphi XE2 之 FireMonkey 入门(22) - 数据绑定: BindingSource、BindingName、FindBinding()、Binding[]

    在窗体上添加 TrackBar1.Edit1.Label1, 然后设置属性(可在设计时): procedure TForm1.FormCreate(Sender: TObject); begin   ...

  9. Delphi XE2 之 FireMonkey 入门(24) - 数据绑定: TBindingsList: TBindExpression.Direction

    在学习 BindingSource 属性时, 可以让两个控件互为绑定源; TBindExpression 对应的功能是 Direction 属性. 先在窗体上添加 Edit1.Edit2.Bindin ...

随机推荐

  1. 使用CXF开发WebService程序的总结(六):结合拦截器使用

    1. 使用CXF提供的拦截器 拦截器在我看来分为两端两向,两端分为:客户端和服务端,两向(方向)分为:进(in)和出(out),即大致四类拦截器. 在这里利用cxf提供的 日志拦截器举例 1.1 在服 ...

  2. time、date、datetime、timestamp和year

    在此声明mysql数据库 时间上总共有五中表示方法:它们分别是 time.date.datetime.timestamp和year. time : “hh:mm:ss”格式表示的时间值,格式显示TIM ...

  3. hbase权限控制

    HBase的权限管理依赖协协处理器.所以我们需要配置以下参数: hbase.superuser=hbase hbase.coprocessor.region.classes=org.apache.ha ...

  4. info - 阅读 info 文档

    SYNOPSIS 总览 info [OPTION]... [MENU-ITEM...] DESCRIPTION 描述 阅读 info 格式的文档. OPTIONS 选项 --apropos=STRIN ...

  5. PAT Basic 1043 输出PATest (20 分)

    给定一个长度不超过 ​ ​​ 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符 ...

  6. Generative Adversarial Nets(GAN Tensorflow)

    Generative Adversarial Nets(简称GAN)是一种非常流行的神经网络. 它最初是由Ian Goodfellow等人在NIPS 2014论文中介绍的. 这篇论文引发了很多关于神经 ...

  7. 单调队列优化DP || [SCOI2010]股票交易 || BZOJ 1855 || Luogu P2569

    题面:P2569 [SCOI2010]股票交易 题解: F[i][j]表示前i天,目前手中有j股的最大收入Case 1:第i天是第一次购买股票F[i][j]=-j*AP[i]; (1<=j< ...

  8. imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable

    错误: imagecreatefromstring(): Empty string or invalid image 或者 imagesx() expects parameter 1 to be re ...

  9. init.uniform / unit.normal

    均匀分布nn.init.uniform(tensor,a=0,b=1)tensor -n维的torch.Tensora 均匀分布的下界,默认值为0b 均匀分布的上界,默认值为1 正态分布torcn.n ...

  10. 重置grafana密码

    [root@host~]# sqlite3 /var/lib/grafana/grafana.db SQLite version 3.7.17 2013-05-20 00:56:22 Enter &q ...