How to anchor columns in a TStringGrid? lazarus / 31-LCL ---------------------------------------------------------------------- edvard · 2015-04-16 05:44 Or is it even possible? I have a TStringGrid that I want to keep only 4 columns: 'Name', 'Start', 'Stop', 'Total'.  I have the TStringGrid component anchored to the bottom and sides of the form, so when the form is resized, the component also.  Trouble is, I want the columns to resize with the form as well, but resizing only shows more rows or columns (or in my case, blank space because there are no more columns).  I don't mind a resize revealing more rows, but the columns I want to follow resizing the form.  Is there such a way?  Or is there a better component for this kind of thing? Here's what I'm talking about: ---------------------------------------------------------------------- munair · 2015-04-16 06:06 You should be able to adjust the column widths in the right ratio. Let's say your first column is 70% of total width and the other three 10% each. Then use a matrix to adjust these widths according to the total width. Let's say total width is 500: 500 70 10 10 10 350 50 50 50 Do the calculation in a procedure and place it in OnResize ---------------------------------------------------------------------- balazsszekely · 2015-04-16 06:25 Usually only one column is resized, but you can implement Artie's idea too. Code: [Select]procedure AutoSnapColumn(StringGrid: TStringGrid; const Col: Integer); var   I: Integer;   TotWidth: Integer; begin   TotWidth := 0;   for I := 0 to StringGrid.ColCount - 1 do   begin     if I <> Col then       TotWidth := TotWidth + StringGrid.ColWidths[I];   end;   StringGrid.ColWidths[Col] := StringGrid.Width - TotWidth - 5; end; procedure TForm1.StringGrid1Resize(Sender: TObject); begin   AutoSnapColumn(StringGrid1, 0); end; On some widgetset you may need a timer, to prevent freeze. Set timer interval to 100, enabled property to false. Code: [Select]procedure TForm1.StringGrid1Resize(Sender: TObject); begin   if Timer1.Enabled then     Exit;   Timer1.Enabled := True; end; procedure TForm1.Timer1Timer(Sender: TObject); begin   AutoSnapColumn(StringGrid1, 0);   Timer1.Enabled := False; end; ---------------------------------------------------------------------- taazz · 2015-04-16 07:22 I don't think what you are showing can be done automatically you will have to write some code inside the oresize event to size the last column to fill the rest of the area. There is the AutofillColumns property though which will resize all columns to always fill the entire client area, that might actually be enough for your needs. ---------------------------------------------------------------------- BitBangerUSA · 2015-04-16 15:11 after a quick check, you *might* find that setting Anchors.akLeft and Anchors.akRight to True and the TStringGrid.Width set from within the OnResize trigger to give you what you want. for info, i've seen that the column size calcs in the design-time Grid code is a bit odd. for example a grid width of 1000 with 8 columns, the LFM file will have the first 7 column widths at 124 and the 8th at 128. i believe that this is in the base Grid code and haven't attempt to find what versions of Lazarus/FPC are affected... oops... the reference above to TStringGrid.Width should be the Forms width and the Forms OnResize trigger... i am now fiddling with these things in one of my Projects... your post has lead me to realize a better way to do things. ---------------------------------------------------------------------- BitBangerUSA · 2015-04-16 17:43 guess i don't understand what the Grid anchors - top, bottom, left, right - do. my project doesn't seem to be effected by the settings. however, it now allows the user to resize the form at will and the grid display automatically sizes all the columns and thus the entire grid. as i set the grid width on the fly in the Form's OnResize, it keeps the grid entirely visible on the form - although the text in cells gets chopped if the column width is too small. Edit Apologies - i have code that sizes the Grid Display, which effective makes the Grid Anchors settings have no effect in my Project - effectively doing my own 'anchoring' . IOW, my posting in this thread may just be a distraction, as the OP does want anchoring behavior. Edit #2 After looking at edvards image and original posting again, it seems i have my program doing exactly what he wants. the key is to set the Grid Width and/or Height in the Forms OnResize Event handler. Edit #3 taazz is right - and quicker and less wordy than me... i shut up now. ---------------------------------------------------------------------- edvard · 2015-04-17 00:23 I thought of doing OnResize about 6 minutes (approximately) after posting this, so thanks all for confirming my afterthought, though I was hoping for something a bit more automatic. Anyways, I only want the Name column to resize, so this should be fairly easy.  Thanks err'body! ---------------------------------------------------------------------- edvard · 2015-04-17 05:07 EUREKA! Poking around in TStringGrid in the object inspector, I noticed this: AutoFillColumns http://lazarus- ccr.sourceforge.net/docs/lcl/grids/tcustomgrid.autofillcolumns.html QuoteWhen this option is turned on, the grid will resize all columns to fill the grid's client width. What columns are resized is determined by the following conditions: 1. Fixed Columns are not resized. 2. If the grid has Custom Columns, all columns with SizePriority=0 are not resized. Note that Custom Columns are initially created with SizePriority=1. For TDbGrid, which automatically adds Custom Columns, this means the user will not be able to resize such columns using the mouse. So, I just set 'SizePriority' to 0 for the 'Begin', 'End', and 'Total' columns, but leave SizePriority 1 for the 'Name' column, and ...VOILÀ!! Now I pull the window edge and only the 'Name' column resizes, awesome! ---------------------------------------------------------------------- howardpc · 2015-04-17 07:12 taazz pointed this out to you (though not with the details) in reply #3 ---------------------------------------------------------------------- edvard · 2015-04-18 02:39 Why so he did.  I think I did try what he suggested, but something wasn't working... like you said, "the details", and I dropped it.  I found it again while clicking EVERY property in the Object Inspector and reading what the mini-help said about it, then I read about the SizePriority setting, and it all went over the waterfall from there.  Thanks, Taazz... sorry I dropped your suggestion so early, it was actually exactly what I was looking for.