The value of the sum of selected cells in stringgrid

How can I get the sum value of selected cells or range in stringgrid ? Note that sometimes these cells contain string values!

I try GridCoord , but this does not work well, because sometimes there are "hidden columns".

 procedure TMainShowForm.StgSelectionChanged(Sender: TObject; ALeft, ATop, ARight, ABottom: Integer); var i: Integer; gc: TGridCoord; sum:double; begin for i := 1 to stg.SelectedCellsCount do begin gc := stg.SelectedCell[i - 1]; sum:=sum+stg.floats[(gc.X),(gc.Y)]; end; AdvOfficeStatusBar1.Panels[0].Text:='Sum = '+ formatfloat('#,##0.',sum); AdvOfficeStatusBar1.Panels[1].Text:='Count = '+ inttostr(stg.SelectedCellsCount); end; 
+6
source share
1 answer

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; 
+8
source

Source: https://habr.com/ru/post/927292/


All Articles