|
Resizing a column with the mouse
DefaultColWidth property
A practical example
Auto-size your StringGrid The following Delphi source code demonstrates how to automatically adapt the width of a column to the width of its widest cell contents. It's "auto-sizing" of a column as it were: procedure TForm1.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
i, W, WMax: integer;
begin
WMax := 0;
for i := 0 to (Grid.RowCount - 1) do begin
W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
if W > WMax then
WMax := W;
end;
Grid.ColWidths[Column] := WMax + 5;
end;
How to "auto size" the first column with this procedure: procedure TForm1.Button1Click(Sender: TObject); begin AutoSizeCol(StringGrid1, 0); end; How to "autosize" all of the columns: procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to StringGrid1.ColCount - 1 do
AutoSizeCol(StringGrid1, 0);
end;
Adapt the StringGrid column widths to the Header row Here's another interesting procedure, that adjusts the column widths to the column "headers". By headers we mean: the cells of the fixed row. Of course, only execute this procedure only after you've set some text into the fixed row cells. procedure TForm1.SizeColsToHeaders(Grid: TStringGrid);
var
Col: integer;
begin
for Col := 0 to (Grid.ColCount - 1) do
Grid.ColWidths[Col] :=
Grid.Canvas.TextWidth(Grid.Cells[Col, 0]) + 5;
end;
|
|
| DelphiLand Club | Members of the DelphiLand Club receive our Crash Course Delphi, plus the fully commented source code of numerous projects, plus guaranteed quick answers from our Delphi Forum. Membership is for life! |
TOP :: Source
Code and Tutorials :: Crash Course Delphi :: Forum
DelphiLand Club
TURBO Delphi Explorer FAQ ::
DC Library :: Tips ::
Downloads :: Links