一、全设计时操作:

先在窗体上放置控件:


  1. DataSource1    : TDataSource;
  2. ClientDataSet1 : TClientDataSet;
  3. Label1        : TLabel;
  4. Edit1          : TEdit;
  5. Memo1          : TMemo;
  6. ImageControl1  : TImageControl;
  7. BindNavigator1 : TBindNavigator;
  8.  
  9. {在连接过程中, 会自动添加下面部件}
  10. BindingsList1              : TBindingsList;
  11. BindScopeDB1                : TBindScopeDB;
  12.  
  13. DBLinkLabel1SpeciesNo1      : TBindDBTextLink;
  14. DBLinkEdit1Category1        : TBindDBEditLink;
  15. DBLinkMemo1Notes1          : TBindDBMemoLink;
  16. DBLinkImageControl1Graphic1 : TBindDBImageLink;

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

连接步骤:


  1. 、置 DataSource1 DataSet 属性为 ClientDataSet1;
  2.  
  3. 、置 ClientDataSet FileName 属性为 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';
  4.  
  5. 、将四个控件分别关联到 biolife.xml 中的字段:
  6.   Label1      -> 右键 -> Link To DB Field... -> 选择 Species No (数字)
  7.   Edit1        -> 右键 -> Link To DB Field... -> 选择 CateGory  (string)
  8.   Memo1        -> 右键 -> Link To DB Field... -> 选择 Notes      (Text)
  9.   ImageControl -> 右键 -> Link To DB Field... -> 选择 Graphic    (Graphics)
  10.  
  11. 、置 BindNavigator1 BindScope 属性为 BindScopeDB1;
  12.  
  13. 、置 ClientDataSet1 Active 属性为 True.

二、运行时连接:


先在窗体上放置控件(其它在运行时完成):

  1. DataSource1    : TDataSource;
  2. ClientDataSet1 : TClientDataSet;
  3. Label1        : TLabel;
  4. Edit1          : TEdit;
  5. Memo1          : TMemo;
  6. ImageControl1  : TImageControl;
  7. BindNavigator1 : TBindNavigator;
  8. BindingsList1  : TBindingsList;
  9. BindScopeDB1  : TBindScopeDB;

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

  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   ClientDataSet1.FileName := 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';
  4.   DataSource1.DataSet := ClientDataSet1;
  5.   BindScopeDB1.DataSource := DataSource1;
  6.   BindNavigator1.BindScope := BindScopeDB1;
  7.  
  8.   with TBindDBTextLink.Create(BindingsList1) do
  9.   begin
  10.     ControlComponent := Label1;
  11.     DataSource := BindScopeDB1;
  12.     FieldName := 'Species No';
  13.   end;
  14.  
  15.   with TBindDBEditLink.Create(BindingsList1) do
  16.   begin
  17.     ControlComponent := Edit1;
  18.     DataSource := BindScopeDB1;
  19.     FieldName := 'Category';
  20.   end;
  21.  
  22.   with TBindDBMemoLink.Create(BindingsList1) do
  23.   begin
  24.     ControlComponent := Memo1;
  25.     DataSource := BindScopeDB1;
  26.     FieldName := 'Notes';
  27.   end;
  28.  
  29.   with TBindDBImageLink.Create(BindingsList1) do
  30.   begin
  31.     ControlComponent := ImageControl1;
  32.     DataSource := BindScopeDB1;
  33.     FieldName := 'Graphic';
  34.   end;
  35.  
  36.   ClientDataSet1.Active := True;
  37. end;

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

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


  1. uses Fmx.Bind.Editors, Data.Bind.DBLinks, Fmx.Bind.DBLinks;
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.   ClientDataSet1.FileName := 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';
  6.   DataSource1.DataSet := ClientDataSet1;
  7.   BindScopeDB1.DataSource := DataSource1;
  8.   BindNavigator1.BindScope := BindScopeDB1;
  9.   ClientDataSet1.Active := True;
  10.  
  11.   with TBindDBGridLink.Create(BindingsList1) do //还可用 TBindGridLink
  12.   begin
  13.     GridControl := StringGrid1;
  14.     DataSource := BindScopeDB1;
  15.     Active := True;
  16.   end;
  17. 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. 日语能力测试N1、N2级听力必备核心词汇—头发篇

    日语能力测试N1.N2级听力必备核心词汇—头发篇 要想在短时间内迅速提高日语听力能力的水平,除了每天练习(用2倍的速度)真题之外,掌握听力的核心词汇也是一个必要的好方法. 髪(かみ)--头发髪型(かみ ...

  2. time、date、datetime、timestamp和year

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

  3. Java面试总结 -2018(补录)

    参考详见:https://blog.csdn.net/jackfrued/article/details/44921941 https://blog.csdn.net/jackfrued/articl ...

  4. intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件

    1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...

  5. AIX中物理卷管理

    1.物理卷管理   1.1物理卷区域的分布 按照磁头在硬盘上的读写速率不同可以把硬盘划分成几个不同级别的区域.因此逻辑卷对应的PP在哪一个级别的区域上,对逻辑卷的读写速率影响很大. 硬盘的截面分为5个 ...

  6. (转) Delete/Truncate删除,释放表空间、降低高水位线、resize释放磁盘空间相关优化

    硬盘空间不足,打算删除数据库中的多余数据,但删除数据后,硬盘硬盘空间不能释放.[delete后用:alter table table_name move    truncate后用:alter tab ...

  7. BFPRT: O(n)最坏时间复杂度找第K大问题

    同时找到最大值与最小值 找到n个元素中的最大/小值,比较次数为n-1, 找到n个元素中的最大值和最小值,可以Two Pass,比较次数为2n-2 也可以One Pass,比较次数至多为\(\left ...

  8. ios银行卡号加入* 并四个一个空格

    +(NSString *)getNewBankNumWitOldBankNum:(NSString *)bankNum{    NSMutableString *mutableStr;    if ( ...

  9. ng-model 和ng-bind的区别

    也就是说 ng-model是绑定html输入的值-->到控制器的变量,输入值变了,控制器对应的变量message的值页变了,这样,在其他地方就可以使用这个变化后的值 ng-bind 是绑定控制器 ...

  10. 基于注解的IOC配置

    1 明确 注解配置和XML配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样. 关于实际的开发中到底是使用XML还是注解,每家公司有着不同的习惯.具体问题具体分析. 2 环境搭建 ...