You are here:

Delphi/Stringgrid column totalling

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?

Michael

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

Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Randy, Much thanks. I've tried the code as is and it works great. I will have to modify it to fit in with my program and perhaps I can request further help if I run into difficulties. This is an excellent service and I'm very pleased to have found it. Michael

Delphi

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