DBGridEh how to calculate the amount based on the unit price and quantity?

After the unit price and the number of input, movement in any direction in the number of cells (vertical and horizontal), the amount can be calculated automatically. How to achieve?

    1.  

      Do not count field in the event UI element, you can calculate the unit price field OnChange event and the number of fields. Assumed that the data set name DataSet, a process to write data in the module:
      Procedure CalcMoney (Sender: of TField);
      the begin
        IF (DataSet.State in dsEditModes) and (. Not DataSet.FieldByName ( 'Qty') IsNull) and (Not the DataSet .FieldByName ( 'Price') IsNull).
       the then
        DataSet.FieldByName ( 'Money') AsCurrency:... = DataSet.FieldByName ( 'Qty') AsFloat * DataSet.FieldByName ( 'Price') AsCurrency;
      End; then the data AfterOpen BeforeClose set of events and added the following code:
      AfterOpen:
        . DataSet.FieldByName ( 'Qty') the OnChange: = CalcMoney;
        DataSet.FieldByName ( 'Price') the OnChange:. = CalcMoney;
      BeforeClose;
        DataSet.FieldByName ( 'Qty' ) .OnChange: = Nil;
        DataSet.FieldByName('Price').OnChange := Nil;
        

       

    2.  

      procedure CalcMoney(Sender: TField);
      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'
        

       

    3.  

      procedure TForm1.CalcMoney(Sender: TField);
      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;我试了,这样可以实现。
        

       

    4.  

      DataSet will be replaced with the name of your own data set.

Guess you like

Origin www.cnblogs.com/jijm123/p/11701080.html