How to get the sum of floating point values โโin a TStringGrid?
For a standard Delphi TStringGrid , for example, as follows:
procedure TForm1.Button1Click(Sender: TObject); var Sum: Double; Val: Double; Col: Integer; Row: Integer; begin Sum := 0; for Col := StringGrid1.Selection.Left to StringGrid1.Selection.Right do for Row := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do if TryStrToFloat(StringGrid1.Cells[Col, Row], Val) then Sum := Sum + Val; ShowMessage('Sum of the selection is ' + FloatToStr(Sum) + '.'); end;
How to get the sum of floating point values โโfor selection (including invisible cells) in TAdvStringGrid?
Therefore, you are most likely using TAdvStringGrid , you can try the following code that has not yet been verified or optimized. So far I have found, you can use the AllFloats property to access all grid cells as a float regardless of hidden columns or rows. Assuming you want to summarize continuous selection after hiding a specific column, you can try this code:
procedure TForm1.Button1Click(Sender: TObject); var Sum: Double; Col: Integer; Row: Integer; begin Sum := 0; for Col := AdvStringGrid1.Selection.Left to AdvStringGrid1.Selection.Right do for Row := AdvStringGrid1.Selection.Top to AdvStringGrid1.Selection.Bottom do Sum := Sum + AdvStringGrid1.AllFloats[Col, Row]; ShowMessage('Sum of the selection is ' + FloatToStr(Sum) + '.'); end;
source share