Jump to content


Photo

CalculateFooter on sum() bug


  • Please log in to reply
5 replies to this topic

#1 LongBeard_Boldy

LongBeard_Boldy
  • Members
  • 11 posts

Posted 13 April 2015 - 06:27 PM

Greetings to Bergsoft community, 

well, this is second time i noticed a weird nextgrid behaviour on calculating footer with sum() function. 

This time i did not succumb to laziness instincts and have bit poked around the sources to find the culprit at fault.

 

version 5.9.50

 

Bug occurs only when populated grid contains mostly invisible rows with few visible ones  in combination with any column  (i think or the summed ones?)  is sorted descending.  

when  CalculateFooter is called again, footer contains wrong values. It does not mater if visibility check is true or false, because in my case invisible rows do contain 0 values.

 

i am no brainiack nor a programmer, temporary patched following subfunction in nxgrid.pas 

 

 

 procedure ColumnSum(Index: Integer; var Sum: Double);
   var
      I: Integer;
   begin
   Sum := 0;
   for I := 0 to RowCount - 1 do
      begin
      if (not VisibleOnly or RowVisible[I]) and IsCalculateRow(I) then
         Sum := Sum + Cell[Index, I].AsFloat;
      end;
   end;

{ procedure ColumnSum(Index: Integer; var Sum: Double);
  var
  I: Integer;
  begin
  for I := 1 to Pred(RowCount) do
  if (not VisibleOnly or RowVisible[I]) and IsCalculateRow(I) then
  Sum := Sum + Cell[Index, I].AsFloat;
  end; }

i probably did broke a dozen other calculation behaviours of this function.... but what the heck, didn't see anything else turnin bad :P

 

forgot to mention an important thing, am using Delphi XE2 with 4th update



#2 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 15 April 2015 - 10:19 PM

Hi,

 

Can you please tell me did you only placed Sum := 0 at the beginning?

 

I will look at it now and fix it. Thanks for reporting!


boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#3 deonvn

deonvn

    Senior Member

  • Honorable Members
  • PipPip
  • 313 posts

Posted 16 April 2015 - 10:41 AM

Hi Boki

 

Before you change anything in the ColumnSum procedure, I don't think that is the problem.

 

The ColumnSum procedure was written the way that it is because the Sum parameter will already contain the value of the first visible row passed to it from the CalculateColumn function. That is also why the "for" loop in ColumnSum starts from 1 and not from 0 - because the value of this column for the first visible row has already been passed into this procedure from the CalculateColumn function.

 

I think the real problem is here:

  function CalculateColumn(Index: Integer; FormulaKind: TFormulaKind): Double;
  var
    i: Integer;
    FormulaSum: Double;
    s: WideString;
    sl: TStringList;
  begin
    FormulaSum := 0;

    if (not VisibleOnly or RowVisible[FFirstVisibleRow])
      and (FormulaKind <> fkCount)
      and (FormulaKind <> fkCustom)
        then FormulaSum := Cell[Index, FFirstVisibleRow].AsFloat;

Why are you checking RowVisible(FFirstVisibleRow)? Won't that always return True? Also, the value of FFirstVisibleRow doesn't stay correct if you re-sort the column - the next time you call CalculateFooter, FFirstVisibleRow will be zero (even if Row 0 is not visible).

 

If this is the source of the problem, then changing the ColumnSum procedure will not fix it. You will still have the same problem for columns using fkMaximum and fkMinimum as well.

 

Perhaps it would be better to remove the:

    if (not VisibleOnly or RowVisible[FFirstVisibleRow])
      and (FormulaKind <> fkCount)
      and (FormulaKind <> fkCustom)
        then FormulaSum := Cell[Index, FFirstVisibleRow].AsFloat;

from the CalculateColumn function completely and then change the "for" loops in fkMaximum, fkMinimum and ColumnSum from:

for i := 1 to Pred(RowCount) do

to

for i := 0 to Pred(RowCount) do

so that they always loop through all rows and not skip the first visible row. (Then there will also be no more need to pass a var Parameter to ColumnSum, as the Sum will then always start from zero. In fact, ColumnSum can then be changed to a function that just returns the Sum).

 

PS: As mentioned above, there is also a bug where the value of FFirstVisibleRow is not always correct. Using NextGrid.RowVisible[0] := False will update FFirstVisibleRow the first time, but FFirstVisibleRow is then reset to zero again (when the column is sorted, I think). Using NextGrid.Row[0].Visible := False doesn't update FFirstVisibleRow at all - it will still be zero.

 

Cheers



#4 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 16 April 2015 - 10:37 PM

Hi Deon,

 

Thanks!

 

I have do the similar yesterday; I have added StartIndex for ColumnSum function. I will look once again at your solution and decide what to do.


boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#5 LongBeard_Boldy

LongBeard_Boldy
  • Members
  • 11 posts

Posted 17 April 2015 - 11:27 AM

Had similar thoughts as deonvn, but wasn't sure , thanks for explaining.

Boki,  when you have put your "pipe wrench" aside, cud you please put fix available for us? :)

 

Many thanks in advance.



#6 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 17 April 2015 - 09:28 PM

Hi,

 

Yesterday I have release update with this fix. I think that I will use Deon's post to code it more robust.


boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users