When saving a form with a TDBGrid having a column with Visible = false, the column's Width property is not saved, and the original value of the Width property is lost.
The context is a TColumn in a standard VCL TDBGrid control on a form (Delphi 10.1 Berlin). If the Visible property of the column is set to false in the object inspector, the Width property immediately goes to -1. Setting Visible back to true, restores the previous value of Width.
However, if the form is saved while the column's Visible property is false, and the form is closed and reopened, then the same column's Visible property is set to true again, the Width always comes back as 163839. This of course hides any other columns to the right, and messes up scrolling, which rightly confuses non-technical users.
I suspect it is a VCL bug, since the property getter for the TColumn.Width property (in unit Vcl.DBGrids) is as follows:
unit Vcl.DBGrids;
...
function TColumn.GetWidth: Integer;
begin
if not Showing then // depends on Visible property
Result := -1 // obviously streamed to .DFM
else if cvWidth in FAssignedValues then
Result := FWidth
else
Result := DefaultWidth;
end;
Is this a VCL bug, or am I using it wrong?
PS: does this have anything to do with TColumn.AssignedValues? Or is the built-in Delphi DBGrid just flawed.
Edit: My app allows the user to resize, reorder and show/hide columns in a main results grid at run-time, but also to restore back to defaults. I'm using the grid at design-time as the "master" when restoring defaults, i.e. default column widths and order, and also which columns are visible by default. But losing the column Width for columns hidden by default is messing with that.