You are here:

Delphi/Stringgrid column totalling

Advertisement


Question
Hi,
I have a Stringgrid column containing letters of the Alphabet.
Using Delphi - how can i do a sort of Excel Countif thing to count the occurance of certain letters?

Thanks in advance

Michael

Answer
I'm not 100% if this is what you are after, but should be close.


function CountIf(Expression: boolean): integer;
begin
 Result := Ord(Expression);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
 i: integer;
 TotalCount: integer;
begin
 Randomize;
 StringGrid1.ColCount := 3;
 StringGrid1.RowCount := 30;
 StringGrid1.Cells[1, 0] := 'A';
 StringGrid1.Cells[2, 0] := 'B';

 for i := 1 to StringGrid1.RowCount - 2 do begin
   StringGrid1.Cells[0, i] := 'Line '+IntToStr(i);
   StringGrid1.Cells[1, i] := Chr(Random(26) + 65);
   StringGrid1.Cells[2, i] := IntToStr(CountIf(StringGrid1.Cells[1, i] = 'A'));
 end;

 TotalCount := 0;
 for i := 1 to StringGrid1.RowCount - 2 do begin
   TotalCount := TotalCount + StrToInt(StringGrid1.Cells[2, i]);
 end;
 StringGrid1.Cells[0, StringGrid1.RowCount - 1] := 'Total';
 StringGrid1.Cells[1, StringGrid1.RowCount - 1] := 'CountIf(A = ''A'')';
 StringGrid1.Cells[2, StringGrid1.RowCount - 1] := IntToStr(TotalCount);
end;


Randy

Delphi

All Answers


Answers by Expert:


Ask Experts

Volunteer


Randy Sill

Expertise

Turbo Pascal and Delphi developer since 1986. Strengths - UI, Windows API, Database, SQL, Internet, Threading, NT Services.

Experience

Turbo Pascal and Delphi developer since 1986.

Education/Credentials
Delphi 5 Certification, Borland

Awards and Honors
2005 Indiana IT/Software Million Dollar Award, Key Technical Contributor

©2016 About.com. All rights reserved.