6

選択したセルまたは範囲の合計値を取得するにはどうすればよいstringgridですか? これらのセルには文字列値が含まれている場合があることに注意してください。

を試してみGridCoordましたが、「隠し列」が時々あるのでうまくいきません。

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;
4

1 に答える 1

8

TStringGrid で選択した浮動小数点値の合計を取得する方法は?

たとえば、標準の DelphiTStringGridの場合、次のようにします。

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;

TAdvStringGrid で選択範囲 (非表示のセルを含む) の浮動小数点値の合計を取得する方法は?

したがって、おそらくTAdvStringGrid次の未テストまたは最適化されたコードを試すことができます。これまでのところ、AllFloatsプロパティを使用して、非表示の列や行に関係なく、すべてのグリッド セルに float としてアクセスできます。特定の列を非表示にした後に連続選択を合計したいと仮定すると、次のコードを試すことができます。

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;
于 2012-10-09T20:25:17.960 に答える