unit Unit1;

unit StepTwo;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, IB_ConnectionBar, Grids, IB_Grid, ComCtrls, IB_NavigationBar,
  IB_DatasetBar, IB_Components, IB_Process, IB_Script,
  IB_Session; //-----****This is very important*****------
type
  TGrid_Form = class(TForm)
    IB_ConnectionBar1: TIB_ConnectionBar;
    IB_DatasetBar1: TIB_DatasetBar;
    IB_NavigationBar1: TIB_NavigationBar;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    MyGrid: TIB_Grid;
    GridConnection: TIB_Connection;
    ScMain: TIB_Script;
    GridTransaction: TIB_TransactionSingle;
    qryGrid: TIB_Query;
    srcGrid: TIB_DataSource;
    procedure GridConnectionBeforeConnect(Sender: TIB_Connection);
    procedure qryGridCalculateField(Sender: TIB_Statement; ARow: TIB_Row;
      AField: TIB_Column);
  private
{ Private declarations }
  public
{ Public declarations }
  end;
var
  Grid_Form: TGrid_Form;
implementation
{$R *.DFM}

procedure TGrid_Form.GridConnectionBeforeConnect(Sender: TIB_Connection);
begin
  with GridConnection do begin
    if (Protocol = cpLocal) and not FileExists(DatabaseName) then begin
      scMain.Execute;
    end;
  end;
end;

procedure TGrid_Form.qryGridCalculateField(Sender: TIB_Statement;
  ARow: TIB_Row; AField: TIB_Column);
var
  lvVal: Double;
begin
  inherited;
  with AField do begin
    if FieldName = 'C_LOSS' then begin
      with ARow do begin
        lvVal := ByName('COST_PRICE').AsDouble;
        if lvVal = 0 then begin
          lvVal := 0;
        end else begin
          lvVal := (lvVal - ByName('CURR_VALUE').AsDouble) * 100 / lvVal;
        end; { if }
      end; { with }
      AsInteger := Trunc(lvVal);
    end; { if }
  end; { with }
end;
end.