3

Given a nested TClientDataSet, how could I find the link field name on the detail TClientDataSet?

I'm copying data from one TClientDataSet to another (record by record) and I would like to automatically ignore the link field.

I could also copy the data using the TClientDataSet.Data property but I still would need to clear the link and key fields.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Valdir
  • 31
  • 1
  • 4

1 Answers1

0

You can check the "DataSetField" property of the detail/nested dataset, or access the "NestedDataSet" property of the master dataset.

Sample code to get the "link" field name:

function GetCDSDLinkFieldName(cds: TClientDataSet): string;
var
  i: Integer;
  cdsDetail: TClientDataSet;
begin
  Result := EmptyStr;
  cdsDetail := nil;
  if Assigned(cds.DataSetField) then
    cdsDetail := cds;
  if not Assigned(cdsDetail) and (cds.FieldCount > 0) then
  begin
    i := 0;
    while not Assigned(cdsDetail) and (i < cds.FieldCount) do
    begin
      if cds.Fields[i].DataType = ftDataSet then
        cdsDetail := TClientDataSet(TDataSetField(cds.Fields[i]).NestedDataSet);
      Inc(i);
    end;
  end;
  if Assigned(cdsDetail) then
    Result := cdsDetail.DataSetField.FieldName;
end;

Invoking example:

procedure ...
begin
  ShowMessage(GetCDSDLinkFieldName(cdsMaster));
  ShowMessage(GetCDSDLinkFieldName(cdsDetail));
end;

P.S.: 2 years later I don't believe this answer will help the author of the question, but maybe can help others that search for the same subject.

jfoliveira
  • 2,138
  • 14
  • 25
  • Sorry to take three years to comment about your answer. But I was looking for the name of the link field, not the _DataSetField_ field name itself. For example in a master-detail relationship with _ID, PARENT_ID_ in the detail _DataSet_ I would like to find the _PARENT_ID_ field name. – Valdir Oct 23 '15 at 17:53