Experimente a seguinte sugestão:

 

procedure SetupColumnWidths(ADBGrid: TDBGrid);

 

  function CreateDisplayDC: Windows.HDC;

  begin

    Result := CreateDC('DISPLAY', nil, nil, nil)

  end;

 

  function StringExtent(const S: string; const Font: TFont): TSize;

  var

    Canvas: TCanvas; // canvas used to measure text extent

  begin

    Assert(Assigned(Font));

    Canvas := TCanvas.Create;

    try

      Canvas.Handle := CreateDisplayDC;

      try

        Canvas.Font := Font;

        Result := Canvas.TextExtent(S);

      finally

        DeleteDC(Canvas.Handle);

        Canvas.Handle := 0;

      end;

    finally

      Canvas.Free;

    end;

  end;

 

var

  i, cnt: integer;

  lFld: TField;

  lCol: TColumn;

 

begin

  cnt := ADBGrid.Columns.Count;

  for i := 0 to cnt - 1 do

  begin

    lCol := ADBGrid.Columns[i];

    lFld := lCol.Field;

    if lFld is TNumericField then

      lCol.Width := StringExtent(lFld.EditMask, ADBGrid.Font)

    else

      lCol.Width := StringExtent(StringOfChar('Z', lFld.DisplayWidth),
ADBGrid.Font);

  end;

end;

 

Não sei o q vc quer dizer com ‘fórmula utilizável’, mas de qualquer forma
não vejo nenhuma solução pro seu problema que não envolva o uso da
propriedade DisplayWidth.

 

Sds.

 

 

De: delphi-br@yahoogrupos.com.br [mailto:delphi...@yahoogrupos.com.br] Em
nome de Wilson, Stephen
Enviada em: segunda-feira, 30 de novembro de 2009 11:50
Para: delphi-br@yahoogrupos.com.br
Assunto: [delphi-br] Largura de colunas de DBGrid

 

  

Senhores, 

Alguem tem uma formula para calcular a largura ideal das colunas em um
DBGrid em tempo de execucao? A consulta que alimenta o DBGrid produz campos
diferentes de cada execucao.

No momento, isso funciona:

for i:= 0 to DBGrid1.Fieldcount-1 do
DBGrid1.Columns[i].Width:= Ceil(DM.Q_Report.Fields[i].DisplayWidth * 2.5) ;

.... mas gostaria de ter uma formula utilizavel. Ficaria muito grato por
quaisquer sugestoes.

Att.

Steve

***************************************************************************

This e-mail and any files transmitted with it are confidential. If you are 
not the intended recipient, any reading, printing, storage, disclosure, 
copying or any other action taken in respect of this e-mail is prohibited 
and may be unlawful. If you are not the intended recipient, please notify 
the sender immediately by using the reply function and then permanently 
delete what you have received.

Content of emails received by this Trust will be subject to disclosure 
under the Freedom of Information Act 2000, subject to the specified 
exemptions, including the Data Protection Act 1998 and Caldicott Guardian 
principles.

This footnote also confirms that, unless otherwise stated, this email 
message has been swept by Sophos Anti-virus for the presence of computer 
viruses.

** Please consider the environment before printing this email. **

***************************************************************************





[As partes desta mensagem que não continham texto foram removidas]

Responder a