Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TAdvSpreadGrid
How to add formula support in TAdvSpreadGrid to access DB tables

With this small DB sample mathlib, the concept is demonstrated to add formula support in TAdvSpreadGrid to access DB tables:



type

  TDBMathLib = class(TMathLib)
  private
    { Private declarations }
    FDataSource: TDataSource;
  protected
    { Protected declarations }
    procedure Notification(AComponent: TComponent; AOperation: TOperation); override;
  public
    { Public declarations }
    function HandlesStrFunction(FuncName:string):Boolean; override;
    function CalcStrFunction(FuncName:string;Params:TStringList;var ErrType,ErrParam: Integer):string; override;
  published
    { Published declarations }
    property DataSource: TDataSource read FDataSource write FDataSource;
  end;


implementation

{ TDBMathLib }

function TDBMathLib.CalcStrFunction(FuncName: string; Params: TStringList;
  var ErrType, ErrParam: Integer): string;
var
  s: string;
  n,e: integer;
  fld: TField;
begin
  if (FuncName = ''DBV'') then
  begin
    if Params.Count <> 2 then
    begin
      ErrType := Error_InvalidNrOfParams;
      Exit;
    end;

    if not Assigned(DataSource) then
    begin
      ErrType := Error_NoDataSource;
      Exit;
    end;

    if not Assigned(DataSource.DataSet) then
    begin
      ErrType := Error_NoDataSet;
      Exit;
    end;

    if not DataSource.DataSet.Active then
    begin
      ErrType := Error_NoDataSetActive;
      Exit;
    end;

    s := Params.Strings[0]; // DB FIELD value

    fld := DataSource.DataSet.FieldByName(s);

    if not Assigned(fld) then
    begin
      ErrType := Error_InvalidValue;
      ErrParam := 1;
    end
    else
    begin
      val(Params.Strings[1],n, e);

      DataSource.DataSet.First;
      DataSource.DataSet.MoveBy(n);

      Result := fld.AsString;
    end;
  end;
end;

function TDBMathLib.HandlesStrFunction(FuncName: string): Boolean;
begin
  Result := FuncName = ''DBV'';
end;

procedure TDBMathLib.Notification(AComponent: TComponent;
  AOperation: TOperation);
begin
  inherited;
  if (AOperation = opRemove) and (AComponent = FDataSource) then
    FDataSource := nil;
end;