如何将汇总字段值作为货币进行访问?

时间:2013-01-07 12:23:33

标签: delphi aggregate currency tclientdataset

当我尝试访问聚合字段(SUM_TOTAL)的值时,如下所示:

DM.cdsTOTAL.AsCurrency := DM.cdsItemSUM_TOTAL.AsCurrency;

发生以下错误:

无法访问字段'SUM_TOTAL'作为float类型。

我的临时解决方案是:

DM.cdsTOTAL.AsCurrency := StrToCurrDef(DM.cdsItemSUM_TOTAL.AsString, 0);

还有其他建议吗?

3 个答案:

答案 0 :(得分:2)

这就是TAggregateField如何设计不足。请参阅db.pas中的实现细节,以了解它不会覆盖引发上述AccessError的基础TField.GetAs ...系列

答案 1 :(得分:1)

在黑暗中拍摄:

begin
  If DM.cdsItemSUM_TOTAL.IsNull then
  begin 
    DM.cdsTOTAL.AsCurrency := 0;
  end 
  else
  begin
     DM.cdsTOTAL.AsCurrency := Currency(DM.cdsItemSUM_TOTAL.Value);
  end;
end;

由于该值是浮点数(可能是一个双精度数),我认为它可以转换为货币变量。

答案 2 :(得分:1)

您可以为TAggregateField实施Class Helpers,如果TAggregate需要

type
  TAggregateHelper=Class Helper for TAggregate
  private
    function GetCurrency: Currency;
  published
    Property asCurrency:Currency read GetCurrency;
  End;

  TAggregateFieldHelper=Class Helper for TAggregateField
  private
    function GetCurrency: Currency;
  published
    Property asCurrency:Currency read GetCurrency;
  End;


implementation



{ TAggregateHelper }

function TAggregateHelper.GetCurrency: Currency;
begin
  if not VarIsNull(Value) then
     Result := Value
  else Result := Value;
end;
{ TAggregateFieldHelper }

function TAggregateFieldHelper.GetCurrency: Currency;
begin
  if not VarIsNull(Value) then
     Result := Value
  else Result := Value;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Showmessage(FloatToStr(CDSTotal.AsCurrency) + '-' + FloatToStr(CDS.Aggregat[0].asCurrency));
end;