Delphi:应用更新时访问嵌套的数据集主信息

时间:2011-06-02 14:53:51

标签: delphi tclientdataset

在将更新应用到嵌套数据集时,是否可以在提供程序的MyField.NewValue事件中访问父数据集信息(如BeforeUpdateRecord)?

原因:

当我将更新应用于具有嵌套详细信息的CDS时,主PK由基础查询(TIBCQuery)生成并传播到主CDS。

但由于BeforeUpdateRecord中的字段已更新,因此新密钥在详细信息的AfterUpdateRecord中不可见:

DeltaDS.FieldByName(FieldName).NewValue := SourceDS.FieldByName(FieldName).NewValue) 

并且delta尚未合并。

看起来DeltaDS事件的BeforeUpdateRecord参数仅包含对详细信息进行调用时嵌套数据集的信息。

如果我可以做的事情会很好:

DeltaDS.ParentDS.FieldByName('FIELDNAME').NewValue.

修改

使用嵌套数据集时,BeforeUpdateRecord事件被调用两次,一次用于主数据,一次用于详细信息(如果我们有两个记录)。当为详细信息调用事件时,是否有办法访问DeltaDS中包含的主信息?

由于尚未合并更改,我们无法访问主CDS的数据。我希望这不会增加更多的混乱。

1 个答案:

答案 0 :(得分:2)

您可以使用提供商的Resolver查找相应的TUpdateTree

function FindDeltaUpdateTree(Tree: TUpdateTree; DeltaDS: TCustomClientDataSet): TUpdateTree;
var
  I: Integer;
begin
  Result := nil;
  if Tree.Delta = DeltaDS then
    Result := Tree
  else
    for I := 0 to Tree.DetailCount - 1 do
    begin
      Result := FindDeltaUpdateTree(Tree.Details[I], DeltaDS);
      if Assigned(Result) then
        Break;
    end;
end;

您可以在OnBeforeUpdate处理程序中使用它:

var
  Tree, ParentTree: TUpdateTree;
begin
  if SourceDS = MyDetailDataSet then
  begin
    Tree := FindDeltaUpdateTree(TDataSetProvider(Sender).Resolver.UpdateTree, DeltaDS);
    if Assigned(Tree) then
    begin
      ParentTree := Tree.Parent;
      // here you can use ParentTree.Source (the dataset) and ParentTree.Delta (the delta)
    end;
  end;
end;
相关问题