Por cada columna del dbgrid tienes que recorrer todos los registros y ver la longitud máxima, y despues asignar ese ancho a la columna.
ajustando el DisplayWidth. De este hilo, en lugar de sacar el ancho máximo en caracteres, puedes devolver directamente la cadena más larga, y pasarlo a FontWidthInPixels para saber el ancho exacto.
Código Delphi
[-]
uses Graphics,sysutils,types,windows;
function FontDimensionInPixels(SurfaceToPaint:THandle; Str:string;var TheSize : Tsize):Boolean;
begin
Result := windows.GetTextExtentPoint32(SurfaceToPaint,PChar(str),Length(Str),TheSize)
end;
function FontHeigthInPixels(SurfaceToPaint:THandle; Str:string; var TheHeight : integer):Boolean;
var s:TSize;
begin
Result := FontDimensionInPixels(SurfaceToPaint,Str,S);
if Result then TheHeight := s.cy;
end;
function FontWidthInPixels(SurfaceToPaint:THandle; Str:string;var TheWidth:integer):Boolean;
var s:TSize;
begin
Result := fontdimensioninPixels(SurfaceToPaint,Str,S);
if Result then TheWidth:= s.cx;
end;
SurfaceToPaint sería Dbgrid1.canvas.handle o bien dbgrid1.columns[0].canvas.handle, no lo he probado para un dbgrid con varias columnas, pero debe funcionar.
Un saludo