[英]Delphi Firedac master details multiple
i have 2 table in relation master-details displayed in 2 tdbgrid. 我在2个tdbgrid中显示了2个关系主表中的表。 Now I would like to display each field on the rows of the table details in a Tedit controll, but now i view only the first row of detail in my first group of tedit.
现在,我想在Tedit控件中在表格详细信息的行上显示每个字段,但是现在我在第一组tedit中仅查看详细信息的第一行。
If this is a VCL project (and not a FireMonkey one), you can do this very simply. 如果这是VCL项目(而不是FireMonkey项目),则可以非常简单地执行此操作。
Place a TDBCtrlGrid on your form and set its DataSource
for the detail dataset. 将TDBCtrlGrid放在表单上,并为详细数据集设置其
DataSource
。
In the IDE, the DBCtrlGrid will display a vertical series of panels, one colored solid gray and the others striped. 在IDE中,DBCtrlGrid将显示一系列垂直面板,一个面板为纯灰色,另一个面板为条纹。 Place db-aware controls like TDBEdits on the solid gray panel, set their DataField properties, then compile and run.
将诸如TDBEdits之类的数据库感知控件放在纯灰色面板上,设置其DataField属性,然后编译并运行。 The DBCtrlGrid has an
Orientation
property which you can set to vertical or horizontal, according to your preference. DBCtrlGrid具有一个
Orientation
属性,您可以根据自己的喜好将其设置为垂直或水平。
You should see that at run-time, the DBCtrlGrid populates with as many instances of the panel and the db-aware components it contains as there are detail records, up to the number specified by the DBCtrlGrid's RowCount
property. 你应该看到,在运行时,该DBCtrlGrid与面板,它包含作为有详细记录,直到由DBCtrlGrid的指定数量的数据感知组件的多个实例填充
RowCount
属性。 If you want to ensure that there are enough copies of the panel, you could try setting the RowCount
value to the RecordCount' of the detail dataset in the
AfterScroll` event of the master dataset. 如果你想确保有面板的足够的副本,你可以尝试设置的
RowCount
值到RecordCount' of the detail dataset in the
主数据集的AfterScroll`事件。
On the other hand, if it is a FireMonkey (FMX) project so you need to use LiveBindings (which have no TDBCtrlGrids), it may be possible to do this in a similar manner as you would set up controls to display the fields of the Master dataset, but I've never tried. 另一方面,如果它是一个FireMonkey(FMX)项目,那么您需要使用LiveBindings(不包含TDBCtrlGrids),则可以通过与设置控件以显示控件的字段类似的方式来执行此操作。掌握数据集,但我从未尝试过。
The following minimal VCL project code extract shows the use of a DBCtrlGrid; 以下最小的VCL项目代码摘录显示了DBCtrlGrid的用法;
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, DB, DBClient,
dbcgrids, Mask;
type
TForm1 = class(TForm)
cdsMaster: TClientDataSet;
cdsDetail: TClientDataSet;
dsMaster: TDataSource;
dsDetail: TDataSource;
gMaster: TDBGrid;
dbnavMaster: TDBNavigator;
gDetail: TDBGrid;
dbnavDetail: TDBNavigator;
DBCtrlGrid1: TDBCtrlGrid;
DBEdit1: TDBEdit; // placed in DBCtrlGrid1
DBEdit2: TDBEdit; // placed in DBCtrlGrid1
procedure cdsMasterAfterScroll(DataSet: TDataSet);
procedure FormCreate(Sender: TObject);
private
public
end;
[...]
procedure TForm1.FormCreate(Sender: TObject);
var
i,
j : Integer;
Field : TIntegerField;
begin
DBEdit1.DataField := 'MasterID';
DBEdit2.DataField := 'DetailID';
Field := TIntegerField.Create(Self);
Field.FieldName := 'MasterID';
Field.DataSet := cdsMaster;
cdsMaster.CreateDataSet;
Field := TIntegerField.Create(Self);
Field.FieldName := 'DetailID';
Field.DataSet := cdsDetail;
Field := TIntegerField.Create(Self);
Field.FieldName := 'MasterID';
Field.DataSet := cdsDetail;
cdsDetail.MasterSource := dsMaster;
cdsDetail.MasterFields := 'MasterID';
cdsDetail.IndexFieldNames := 'MasterID;DetailID';
cdsDetail.CreateDataSet;
for i := 1 to 10 do begin
cdsMaster.InsertRecord([i]);
for j := 1 to i do
cdsDetail.InsertRecord([j, i]);
end;
cdsMaster.First;
end;
procedure TForm1.cdsMasterAfterScroll(DataSet: TDataSet);
begin
cdsDetail.DisableControls;
try
DBCtrlGrid1.RowCount := cdsDetail.RecordCount;
finally
cdsDetail.EnableControls;
end;
end;
只需将TDBEdit放在窗体上,然后设置其Datafield属性和详细信息表的Datasource属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.