TAdvStringGrid

Example 70 : Custom group calculation

vcl grid grouping

If there is a need for a special group calculation that is not available in the standard group calculation functions, the method grid.GroupCustomCalc can be used. For each group in the grid, this will trigger the event grid.OnGroupCalc(Sender: TObject; ACol, FromRow, ToRow: Integer; var Res: Double);

The meaning of the parameters is:

ACol : column to perform calculation for FromRow: first row in the group ToRow: last row in the group Res: variable parameter to use to set the result

In this sample, the grid is initialized with random number, is grouped on column 1 and for the first column in the grouped grid the standard deviation is calculated:

The code to achieve this is here:

procedure TForm1.AdvStringGrid1GroupCalc(Sender: TObject; ACol, FromRow,
  ToRow: Integer; var Res: Double);
var
  i: integer;
  d, m, sd: double;
begin

  // calculate mean
  m := 0;
  for i := FromRow to ToRow do
  begin
    m := m + advstringgrid1.Floats[ACol,i];
  end;

  m := m / (ToRow - FromRow + 1); 

  
  sd := 0;
  for i := FromRow to ToRow do
  begin
    sd := sd + sqr(advstringgrid1.Floats[ACol,i] - m);
  end;

  sd := sd / (ToRow - FromRow);
  Res := sqrt(sd);
end;

procedure 
TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin

  advstringgrid1.RowCount := 100;
  advstringgrid1.RandomFill(false,100);
  for i := 1 to advstringgrid1.RowCount - 1 do
    advstringgrid1.Ints[1,i] := random(5);

  advStringGrid1.Grouping.Summary := true;
  AdvStringGrid1.Grouping.MergeHeader := true;
  advstringgrid1.Grouping.ShowGroupCount := true;
  advstringgrid1.Group(1);
  advstringgrid1.GroupCustomCalc(1);
end;


Delphi project & source files for downloading included in the main demos distribution for Delphi.