gpt4 book ai didi

delphi - stringgrid 中选定单元格的总和值

转载 作者:行者123 更新时间:2023-12-03 14:59:36 25 4
gpt4 key购买 nike

如何获取 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;

最佳答案

如何获取 TStringGrid 中所选内容的浮点值之和?

对于标准德尔福 TStringGrid例如这样:

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;

关于delphi - stringgrid 中选定单元格的总和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12807687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com