Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I resize a string grid column to fit the largest text in the cells?

Graphical User Interface

How do I resize a string grid column to fit the largest text in the cells?

by  djjd47130  Posted    (Edited  )
In a String Grid (TStringGrid), you may want to automatically resize a column to fit the longest string under that column. This procedure will do the resizing for you:

Code:
procedure ResizeCol(AGrid: TStringGrid; const ACol: Integer);
const
  MIN_COL_WIDTH = 15;
var
  M, T: Integer;
  X: Integer;
begin
  M:= MIN_COL_WIDTH;
  AGrid.Canvas.Font.Assign(AGrid.Font);
  for X:= 1 to AGrid.RowCount - 1 do begin
    T:= AGrid.Canvas.TextWidth(AGrid.Cells[ACol, X]);
    if T > M then M:= T;
  end;
  AGrid.ColWidths[ACol]:= M + MIN_COL_WIDTH;
end;
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top