Divide by zero exception in TAdvStringGrid

Under rare circumstances, all column widths of a grid can become zero.

The AutoFitColumns procedure doesn't take this into account. Proposed Change (exit when ColW = 0) highlighted in red.

procedure TAdvStringGrid.AutoFitColumns(const DoFixedCells: boolean = true);
...

  if ColCount > 1 then
  begin
    ColW := 0;
    for i := StartCol to EndCol do
      ColW := ColW + ColWidths; <-- all ColWidths[] are  zero

    UseW := AvailWidth - (ScrlW + BrdrW + LineW);

    if (ColW = AvailWidth - (ScrlW + BrdrW)) or (ColW = 0) then
      Exit;

    ratio := UseW / ColW;   <-- Exception occurs here, because ColW is zero

Maybe

if ColW <= AvailWidth - (ScrlW + BrdrW) then
      Exit;

does the trick as well. I didn't investigate further

Thanks for reporting. In this exceptional case that all columns are zero width, there was indeed an issue. We have corrected this and the next update will address this.

If all column widths reach zero, the grid will not recover and autofitcolumns will never grow the column widths again.
This can be reproduced by:
- placing a grid with alClient on a form/panel
- calling TAdvStringGrid.AutoFitColumns from FormResize/PanelResize
- shrinking the form panel so that al column widths reach zero

I think the feature you are after is

grid.ColumnSize.SyncWithGrid = true
With this setting, you do not need to call AutoFitColumns from a resize event.

Thanks, that was a good hint

I'm unhappy of this code:


procedure TAdvStringGrid.AutoFitColumns(const DoFixedCells: boolean = true);
...

if ColW <= AvailWidth - (ScrlW + BrdrW) then
      Exit;

because if my total columns width is lower then available width then is the code breaked and columns aro not fitted to the available space. So i fix the code to

   if (ColW = AvailWidth - (ScrlW + BrdrW)) or (ColW = 0) then
      Exit;

and it works perfectly.

Thanks for your feedback.
That is indeed an improvement for cases where the total column width is smaller than the width of the grid. We will make the adaption accordingly.

Wasn't that my initial suggestion? :scratcheshead:

Yes, it was. And it helped me.