Can a TDBGrid have a default maximum column width? lazarus / 16-Databases ---------------------------------------------------------------------- vfclists · 2016-02-24 00:49 I have noticed that TDBGrid column sizing defaults does not consider the practicality of making a column wider than the grid itself. When some fields are wide, eg 128 upwards, the TDBGrid sesms to default them to the number of characters. Is there some setting that allows a maximum width for the columns default? ---------------------------------------------------------------------- Mike.Cornflake · 2016-02-24 02:39 Interesting.  I was going to say yes Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TDBGrid.Columns[x].MaxWidthHowever, for some reason I'm not using that in my own code, and I can't remember why.  Here's some code I wrote in 2008 that I've been using ever since, and honestly I was assuming now that this code called MaxWidth.  Instead I'm manually setting the widths of each column myself.  (There's some other stuff in there, I usually hide any field ending with 'ID' for instance, my app uses both SQLDB and Zeos, so I'm handling sorting for each).  The Max Width here I've arbitarily set as 400 across all columns) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure InitialiseDBGrid(oGrid: TDBGrid; oDataset: TDataset; bHideIDs: Boolean = False);Var  iTemp: Integer;  oField: TField;  oColumn: TColumn;  iRow: Integer;  iProcessCount: Integer;Begin  If not oDataset.Active Then    Exit;   oDataset.DisableControls;  Try    // Not the correct place, but certainly a convenient place    If Assigned(oDataset) Then      If oDataset Is TZAbstractRODataset Then        TZAbstractRODataset(oDataset).SortType := stIgnored      Else If oDataset Is TSQLQuery Then        TSQLQuery(oDataset).IndexName := '';     oDataset.First;     // First, get the basics right...    oGrid.Canvas.Font.Style := [fsBold];    For iTemp := oGrid.Columns.Count - 1 Downto 0 Do    Begin      oColumn := oGrid.Columns[iTemp];      oField := oColumn.Field;       oField.DisplayLabel := FindReplace(oField.FieldName, '_', ' ');      oColumn.DisplayName := oField.DisplayLabel;       oColumn.Width := oGrid.Canvas.TextWidth(oColumn.DisplayName) + 18;       // Hide the ID columns if so requested      If (bHideIDs) And (Copy(oField.FieldName, Length(oField.FieldName) - 1, 2) =        'ID') Then      Begin      oColumn.Visible := False;        oField.Visible := False;      End;     // Ensure the sort arrows are not displayed by default      oColumn.Title.ImageIndex := -1;    End;     // Next, plough through the visible dataset...    oGrid.Canvas.Font.Style := [];    oDataset.First;    iRow := 0;    iProcessCount := Trunc(oGrid.Height / oGrid.DefaultRowHeight);     // This is all visible records    // Change iProcessCount to oGrid.DataSource.DataSet.RecordCount for all records;     While (Not oDataset.EOF) And (iRow < iProcessCount) Do  Begin      For iTemp := oGrid.Columns.Count - 1 Downto 0 Do      Begin        oColumn := oGrid.Columns[iTemp];        oField := oColumn.Field;         If (oColumn.Visible) And (oColumn.Width < 400) Then          oColumn.Width := Min(400, Max(oColumn.Width,            oGrid.Canvas.TextWidth(oField.DisplayText) + 12));      End;       oDataset.Next;      Inc(iRow);    End;     oDataset.First;  Finally  oDataset.EnableControls;  End;End;  All I can suggest is that you try Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TDBGrid.Columns[x].MaxWidth yourself.  See how it goes. ---------------------------------------------------------------------- vfclists · 2016-02-24 09:30 @Mike.Cornflake I am thinking more in terms of the built-in code itself. The code appears to dumbly to set a width proportional to the size of the underlying column. It would be interesting to see where in the LCL code the defaults are set and fix them there, after which they can be adjusted in the form designer or at runtime. ---------------------------------------------------------------------- vfclists · 2016-02-24 20:32 I think it warrants a feature request. Can anyone give point me the place in the source code where and how the widths of the grid are set when no TColumns are specified? ---------------------------------------------------------------------- Mike.Cornflake · 2016-02-24 22:19 Dafuq?  Somehow previous reply has vanished while I was editing it.  Definitely caffiene shortage. QuoteCan anyone give point me the place in the source code where and how the widths of the grid are set when no TColumns are specified? I'm vague over the exact requirements you're after.  Column sizing is dependent on quite a few different options. Have a look at: dbgrids.pas: procedure TCustomDBGrid.UpdateAutoSizeColumns dbgrids.pas: function CalcColumnFieldWidth grids.pas: function TCustomGrid.GetColWidths GetColWidths shows me there's a DBGrid.DefaultColWidth.  Perhaps that's coming into play? Good luck :-)