dephi 在dbgrid中既可以金额=数量单价 也可以单价=金额数量
在Delphi的DBGrid中,可以通过以下两种方式实现金额=数量*单价 和 单价=金额/数量 的计算:
-
通过事件处理程序实现:
-
在DBGrid上的单元格编辑事件(OnSetEditText)中,使用以下代码实现金额=数量*单价的计算:
procedure TForm1.DBGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string); var Quantity, Price, Amount: Double; begin if (ACol = QuantityColumnIndex) or (ACol = PriceColumnIndex) then begin Quantity := StrToFloat(DBGrid1.Fields[QuantityColumnIndex].AsString); Price := StrToFloat(DBGrid1.Fields[PriceColumnIndex].AsString); Amount := Quantity * Price; DBGrid1.Fields[AmountColumnIndex].AsFloat := Amount; end; end; -
同样,在DBGrid上的单元格编辑事件(OnSetEditText)中,使用以下代码实现单价=金额/数量的计算:
procedure TForm1.DBGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string); var Quantity, Price, Amount: Double; begin if (ACol = QuantityColumnIndex) or (ACol = AmountColumnIndex) then begin Quantity := StrToFloat(DBGrid1.Fields[QuantityColumnIndex].AsString); Amount := StrToFloat(DBGrid1.Fields[AmountColumnIndex].AsString); Price := Amount / Quantity; DBGrid1.Fields[PriceColumnIndex].AsFloat := Price; end; end;
其中,
QuantityColumnIndex、PriceColumnIndex、AmountColumnIndex分别表示数量、单价和金额所在的列索引。 -
-
通过SQL查询计算:
- 使用SQL查询语句,在SELECT语句中使用计算表达式来实现金额=数量*单价 或 单价=金额/数量 的计算,然后将查询结果绑定到DBGrid上显示。
这两种方法都可以实现在DBGrid中动态计算金额和单价的功能,具体选择哪种方法取决于你的需求和实际情况
原文地址: http://www.cveoy.top/t/topic/ilGT 著作权归作者所有。请勿转载和采集!