Frequently Asked Questions
Resizing StringGrid Columns
Question:
Is it possible to automatically size a column in a TStringGrid
component to fit the text of the largest string in the column?
Answer:
The following example demonstrates auto sizing a column in a
TStringGrid Component.
Example:
procedure AutoSizeGridColumn(Grid : TStringGrid;
column : integer);
var
i : integer;
temp : integer;
max : integer;
begin
max := 0;
for i := 0 to (Grid.RowCount - 1) do begin
temp := Grid.Canvas.TextWidth(grid.cells[column, i]);
if temp > max then max := temp;
end;
Grid.ColWidths[column] := Max + Grid.GridLineWidth + 3;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AutoSizeGridColumn(StringGrid1, 1);
end;