计算隐藏的字段

时间:2014-02-21 02:45:58

标签: delphi

我正在隐藏一个字段,以便在显示时(在复选框上选中)它会执行某个计算。

procedure TForm1.cxCheckBox1Click(Sender: TObject);
var
C:TcxGridDBColumn;
begin
if ABSTable1.FieldByName('CENIK_IME').AsString = 'PAK' then begin
C := cxGrid2dbtableview1.GetColumnByFieldName('TT');
if Assigned(C) then C.Visible := not C.Visible;
ABSQuery2.Edit;
ABSQuery2.FieldByName('TOTAL').AsCurrency := (ABSQuery2.FieldByName('TOTAL').AsCurrency) + (ABSQuery2.FieldByName('TT').AsCurrency);
ABSQuery2.Refresh;
end;
end;

问题是每次我选中或取消选中复选框时,我的TOTAL会变得越来越大。有没有办法阻止复选框每次被选中或取消选中时进行求和?

此外,我还有计算查询字段;

procedure TForm1.ABSQuery2CalcFields(DataSet: TDataSet);
begin
ABSQuery2.FieldByName('TT').Value:= (ABSQuery2.FieldByName('DAYS').AsCurrency) * 1.01 ;
end;

这一切都是在Temp表上完成的,该表仅用于场合。内容一直被删除....

1 个答案:

答案 0 :(得分:1)

您是否需要减去ABSQuery2.FieldByName('TT')。如果列被隐藏,则为AsCurrency吗? 如果找到TT,也只会更改Total? 所以:

procedure TForm1.cxCheckBox1Click(Sender: TObject);
var
  C:TcxGridDBColumn;
begin
  if ABSTable1.FieldByName('CENIK_IME').AsString = 'PAK' then begin
    C := cxGrid2dbtableview1.GetColumnByFieldName('TT');

    if Assigned(C) then 
    begin
      C.Visible := not C.Visible;

      ABSQuery2.Edit;  
      if C.Visible then
        ABSQuery2.FieldByName('TOTAL').AsCurrency := (ABSQuery2.FieldByName('TOTAL').AsCurrency) + (ABSQuery2.FieldByName('TT').AsCurrency)
      else
        ABSQuery2.FieldByName('TOTAL').AsCurrency := (ABSQuery2.FieldByName('TOTAL').AsCurrency) - (ABSQuery2.FieldByName('TT').AsCurrency);
      ABSQuery2.Post;
      ABSQuery2.Refresh;
    end;
  end;
end;