Code: |
procedure TForm1.Button1Click(Sender: TObject); { by P. Below } const DEFBORDER = 8; var max, temp, i, n: Integer; begin with Grid do begin Canvas.Font := Font; for n := Low(Columns) to High(Columns) do begin max := 0; for i := 0 to RowCount - 1 do begin temp := Canvas.TextWidth(Cells[Columns[n], i]) + DEFBORDER; if temp > max then max := temp; end; { For } if max > 0 then ColWidths[Columns[n]] := max; end; { For } end; { With } end; {SetGridColumnWidths } |
Code: |
{ This will resize the columns of a TStringGrid / TDrawGrid (text only!) so the text is completely visble. To save some time, it uses the first 10 rows only, but that should be easy to fix, if you need more. }
// we need this to access protected methods type TGridHack = class(TCustomGrid);
procedure ResizeStringGrid(_Grid: TCustomGrid); var Col, Row: integer; Grid: TGridHack; MaxWidth: integer; ColWidth: integer; ColText: string; MaxRow: integer; ColWidths: array of integer; begin Grid := TGridHack(_Grid); SetLength(ColWidths, Grid.ColCount); MaxRow := 10; if MaxRow > Grid.RowCount then MaxRow := Grid.RowCount; for Col := 0 to Grid.ColCount - 1 do begin MaxWidth := 0; for Row := 0 to MaxRow - 1 do begin ColText := Grid.GetEditText(Col, Row); ColWidth := Grid.Canvas.TextWidth(ColText); if ColWidth > MaxWidth then MaxWidth := ColWidth; end; if goVertLine in Grid.Options then Inc(MaxWidth, Grid.GridLineWidth); ColWidths[Col] := MaxWidth + 4; Grid.ColWidths[Col] := ColWidths[Col]; end; end;
|
Просьба писать ваши замечания, наблюдения и все остальное,
что поможет улучшить предоставляемую информацию на этом сайте.
ВСЕ КОММЕНТАРИИ МОДЕРИРУЮТСЯ ВРУЧНУЮ, ТАК ЧТО СПАМИТЬ БЕСПОЛЕЗНО!