随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


一、全设计时操作:

先在窗体上放置控件:
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;

posted on   万一  阅读(8351)  评论(9编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧


点击右上角即可分享
微信分享提示