单价和数量输入完后,在数量单元格往任何方向移动(上下左右),金额都能自动计算。如何实现?
DBGridEh中根据单价和数量如何计算金额?
猜你喜欢
转载自www.cnblogs.com/jijm123/p/11701080.html
今日推荐
周排行
procedure CalcMoney(Sender: TField);
begin
if (DataSet.State in dsEditModes) and (not DataSet.FieldByName('Qty').IsNull) and (not DataSet.FieldByName('Price').IsNull)
then
DataSet.FieldByName('Money').AsCurrency := DataSet.FieldByName('Qty').AsFloat * DataSet.FieldByName('Price').AsCurrency;
end;然后在数据集的AfterOpen和BeforeClose事件中添加如下代码:
AfterOpen:
DataSet.FieldByName('Qty').OnChange := CalcMoney;
DataSet.FieldByName('Price').OnChange := CalcMoney;
BeforeClose;
DataSet.FieldByName('Qty').OnChange := Nil;
DataSet.FieldByName('Price').OnChange := Nil;
begin
if (Sender.DataSet.State in dsEditModes) and (not Sender.DataSet.FieldByName('Counts').IsNull)
and (not Sender.DataSet.FieldByName('Price').IsNull)
then
Sender.DataSet.FieldByName('Money').AsCurrency := Sender.DataSet.FieldByName('Counts').AsFloat * Sender.DataSet.FieldByName('Price').AsCurrency;
end;改成这个之后,编译时提示:
E2009 Incompatible types:'method pointer and regular procedure'
begin
if (Sender.DataSet.FieldByName('Counts').IsNull) then
Sender.DataSet.FieldByName('Counts').AsFloat := 0;
if (Sender.DataSet.FieldByName('Price').IsNull) then
Sender.DataSet.FieldByName('Price').AsCurrency := 0;
if (Sender.DataSet.State in dsEditModes) and (not Sender.DataSet.FieldByName('Counts').IsNull)
and (not Sender.DataSet.FieldByName('Price').IsNull)
then
Sender.DataSet.FieldByName('Money').AsCurrency := Sender.DataSet.FieldByName('Counts').AsFloat * Sender.DataSet.FieldByName('Price').AsCurrency;
end;我试了,这样可以实现。