QuantumGrid - como ajustar o tamanho das coluna proporcionalmente pelo tamanho da tela (cxGrid)

Top  Previous  Next

// estrutura na tela:

Grid

   Nivel - Dados

 

// onde

Grid : TcxGrid

Nivel: TcxGridLevel

Dados: TcxGridDBTableView

 

uses cxGridDBTableView, cxGridTableView, cxGrid, cxGridCustomView, cxGridStyleSheetsPreview, 

     cxStyleSheetEditor, Cxstyles;

 

----------------------------------------

// QuantumGrid - como ajustar o tamanho das coluna proporcionalmente pelo tamanho da tela (cxGrid)

 

// ajustar as colunas para ficarem o menor tamanho possivel sem ocultar as informacoes 

Dados.ApplyBestFit;

 

 

// ajustar as colunas para ficarem o maior tamanho possivel proporcionalmente ao tamanho que

// estao mas ocupando toda a tela sem criar scroll horiz.

var

  I, WidthColVisible, WidthSpaceFree, ColVisible, Last: Integer;

  Percentual: Double;

begin

  WidthColVisible := 0;

  ColVisible      := 0;

  // descobre o tamanho total e quantas colunas estão visiveis

  for I := 0 to Dados.ColumnCount-1 do

    if Dados.Columns[I].Visible then

    begin

      WidthColVisible := WidthColVisible + Dados.Columns[I].Width;

      Inc(ColVisible);

      Last := I;

    end;

  // WidthSpaceFree é o valor que tem que ser "dividido" entre as colunas quer seja negativo, quer seja positivo.

  WidthSpaceFree := TcxGrid(Dados.Control).Width - WidthColVisible - GetSystemMetrics(SM_CXVSCROLL) - 2;

  // tem espaço livre ou falta?

  if WidthSpaceFree <> 0 then

    for I := 0 to Dados.ColumnCount-1 do

      if Dados.Columns[I].Visible then

      begin

        // aqui distribui proporcionalmente para cada coluna de acordo

        // com o percentual que ele representa no total

        Percentual := (Dados.Columns[I].Width * 100) / WidthColVisible;

        Dados.Columns[I].Width := Dados.Columns[I].Width + Trunc((WidthSpaceFree / 100) * Percentual);

        if (Last = I) then

          if (WidthSpaceFree < 0) then

            Dados.Columns[I].Width := Dados.Columns[I].Width - 2;

      end;

end;