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
连接步骤:
、置 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) - 数据绑定: 绑定数据库的更多相关文章
- Delphi XE2 之 FireMonkey 入门(30) - 数据绑定: TBindingsList: TBindExpression 的 OnAssigningValue 事件
Delphi XE2 之 FireMonkey 入门(30) - 数据绑定: TBindingsList: TBindExpression 的 OnAssigningValue 事件 表达式中的函数有 ...
- Delphi XE2 之 FireMonkey 入门(29) - 数据绑定: TBindingsList: 表达式的 Evaluate() 方法
Delphi XE2 之 FireMonkey 入门(29) - 数据绑定: TBindingsList: 表达式的 Evaluate() 方法 TBindingsList 中可能不止一个表达式, 通 ...
- Delphi XE2 之 FireMonkey 入门(27) - 数据绑定: TBindingsList: TBindScope
Delphi XE2 之 FireMonkey 入门(27) - 数据绑定: TBindingsList: TBindScope 如果在编写表达式时, 如果能够随意指认需要的控件就好了(通过 Owne ...
- Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems
Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems 如果要给一对 "源控件" 和 " ...
- Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数
Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数 绑定表达式中可以有简单的运算和字符串连接, 但字符串需放在双引号 ...
- Delphi XE2 之 FireMonkey 入门(32) - 数据绑定: TBindingsList: TBindList、TBindPosition [未完成...]
Delphi XE2 之 FireMonkey 入门(32) - 数据绑定: TBindingsList: TBindList.TBindPosition [未完成...] //待补...
- Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText()、CheckedState()
Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText().CheckedState() 示例构想: 用 ...
- Delphi XE2 之 FireMonkey 入门(22) - 数据绑定: BindingSource、BindingName、FindBinding()、Binding[]
在窗体上添加 TrackBar1.Edit1.Label1, 然后设置属性(可在设计时): procedure TForm1.FormCreate(Sender: TObject); begin ...
- Delphi XE2 之 FireMonkey 入门(24) - 数据绑定: TBindingsList: TBindExpression.Direction
在学习 BindingSource 属性时, 可以让两个控件互为绑定源; TBindExpression 对应的功能是 Direction 属性. 先在窗体上添加 Edit1.Edit2.Bindin ...
随机推荐
- Redis info笔记
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/wufaliang003/article/d ...
- Python3使运行暂停的方法
在Python3中已经有很大一部分语句与Python2不互通了,运行暂停的方法也有所不同. 1.input(); 这种方法不用包含模块,因此这也是最常用的一种暂停手段. Python2中的raw_in ...
- C++ STL(一)string的用法
# 1.string的用法- string在<iostream>的头中就有,但是还是得用<string>的头 ##### 构造函数- str(const char* ch) 直 ...
- SQL函数 Convert,dateadd
1.SQL中CONVERT转化函数的用法 格式:CONVERT(data_type,expression[,style])说明:此样式一般在时间类型(datetime,smalldatetime)与字 ...
- 告别if/else连环写法
1.once Upon a time 在平时的编码过程中,我们大部分新手可能都特别钟情于 if/else连环写法,比如举个简单栗子: 拿订单来说,我们正常的订单类型有多种,那么对应就会生成不同的收款, ...
- Eclipse中配置Python插件
1.点击Window ---> preferences ,左边的标题栏找到Pydev 2. 在Pydev里找到含有Python的选项,点击,在右侧New,弹出框中找到Python的安放目录下的 ...
- python爬虫及结巴分词《攀登者》影评分析
<攀登者>影评爬取及分析 0.项目结构 其中simkai.ttf为字体文件,Windows查看系统自带的字体 C:\Windows\Fonts 一.爬取豆瓣影评数据 # -*- codin ...
- numpy模块、matplotlib模块、pandas模块
目录 1. numpy模块 2. matplotlib模块 3. pandas模块 1. numpy模块 numpy模块的作用 用来做数据分析,对numpy数组(既有行又有列)--矩阵进行科学计算 实 ...
- 微信小程序-饮食日志_开发日志
针对假期作业为父母或者身边的人做一款“小软件”这个课题,由于对 android 开发不熟悉 ,所以决定做一款微信小程序. 项目名称:饮食管理日志 目的:身边的人群对摄入食物热量及消耗不清楚,对健康需求 ...
- 【leetcode】1178. Number of Valid Words for Each Puzzle
题目如下: With respect to a given puzzle string, a word is valid if both the following conditions are sa ...