Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Ancho de UNA columna de TStringGrid de acuerdo a un texto. (https://www.clubdelphi.com/foros/showthread.php?t=80057)

TiammatMX 31-08-2012 18:36:02

Ancho de UNA columna de TStringGrid de acuerdo a un texto.
 
1 Archivos Adjunto(s)
Jóvenes delphineros:

Me encuentro con que estoy rellenando una columna de un TStringGrid con un texto de ésta forma:

Código Delphi [-]
.
.
.
 
         Open;
         while not Eof do
         begin
            Cells[0,iIndiceRen] := Trim(FieldByName('C93_DIETA_STR').AsString);
            RowCount := RowCount + 1;
            iIndiceRen := RowCount - 1;
            Next;
         end;
.
.
.

Lo cual está perfectamente correcto. Al mostrar el TStringGrid en mi forma (imagen adjunta) algunos textos de la primera columna aparecen "cortados" y otros "montados".

¿De qué manera podría darle a la columna extrema izquierda un tamaño suficiente para mostrar EL TEXTO MÁS LARGO por programación?

Por su atención y ayuda, de antemano, gracias.

kapcomx 31-08-2012 18:41:20

lenght
 
Ke tal amigo, se me ocurre que podrias utilizar la funcion length para determinar el ancho de la cadena y el resultado
ingresarselo al width de la columna.

Penosamente no tengo a la mano ejemplos de cuando trabajaba con stringgrids, pero espero te halla podido ayudar

Saludos ...:cool:

ecfisa 31-08-2012 19:11:07

Hola tiammat.

Proba de este modo:
Código Delphi [-]
procedure TForm1.AjustarColumna(aStringGrid: TStringGrid; aDataSet: TDataSet; const aCol: Integer);
var
  i, MaxSize: Integer;
begin
  aStringGrid.RowCount := aDataSet.FieldCount + 1;
  MaxSize := 0;
  for i := 0 to aDataSet.Fields.Count-1 do
    if Canvas.TextWidth(aDataSet.Fields[i].DisplayLabel) > MaxSize then
      MaxSize := Canvas.TextWidth(' '+UpperCase(aDataSet.Fields[i].DisplayLabel)+' ');
  aStringGrid.ColWidths[aCol]:= MaxSize;
  // mostrar
  for i := 0 to aDataset.Fields.Count-1 do
    aStringGrid.Cells[aCol, i+1] := aDataSet.Fields[i].DisplayLabel;
end;

Llamada de ejemplo:
Código Delphi [-]
   AjustarColumna(StringGrid1, IBDataSet1, 0);

Saludos.

viveba 21-06-2014 00:39:06

Supongo ya solucionaste tu problema, para otros foristas interesados, lo siguiente puede ser una solución:

Código Delphi [-]
procedure TForm2.FormCreate(Sender: TObject);
begin
  Grilla.ColCount   := 10;
  Grilla.Cells[0,0] := 'Titulo 1';
  Grilla.Cells[1,0] := 'Titulo 2';
  Grilla.Cells[2,0] := 'Titulo 2';
  Grilla.Cells[3,0] := 'Titulo 4';
  Grilla.Cells[4,0] := 'Titulo 5';
  Grilla.Cells[5,0] := 'Titulo 6';
  Grilla.Cells[6,0] := 'Titulo 7';
  Grilla.Cells[7,0] := 'Titulo 8';
  Grilla.Cells[8,0] := 'Titulo 9';
  Grilla.Cells[9,0] := 'Titulo 10';

  Grilla.ColWidths[0] := 65;
  Grilla.ColWidths[1] := 70;
  Grilla.ColWidths[2] := 70;
  Grilla.ColWidths[3] := Grilla.ColWidths[2];
  Grilla.ColWidths[4] := Grilla.ColWidths[2];
  Grilla.ColWidths[5] := 75;
  Grilla.ColWidths[6] := Grilla.ColWidths[5];
  Grilla.ColWidths[7] := 85;
  Grilla.ColWidths[8] := 70;
end;

procedure TForm2.FormResize(Sender: TObject);
var
  i: byte;
  c: word;

begin
  c := 0;
  for i := 0 to Grilla.ColCount - 2 do
    c := c + Grilla.ColWidths[i];
  Grilla.ColWidths[Grilla.ColCount - 1] := Grilla.Width - c - 35; //35 según sea el ancho del scrollbar de la grilla, su relación al formulario, etc.
end;

//El siguiente código se basa en lo disponible en el material "delphi al límite" (http://delphiallimite.blogspot.com/)

procedure TForm2.GrillaDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  Alineacion : TAlignment;  // Alineación que le vamos a dar al texto
  F          : Cardinal;    // Permite dibujar el texto con DT_END_ELLIPSIS

begin
  with Grilla.Canvas do
  begin // Lo primero es tomar la fuente por defecto que le hemos asignado al componente
    Font.Name := Grilla.Font.Name;
    Font.Size := Grilla.Font.Size;

    if ARow = 0 then begin //todos los títulos al centro
      Font.Style := [fsBold];     // y negrita
      Alineacion := taCenter
    end else
      if ACol = 0 // Si es la columna del 'titulo 1' alineamos a la derecha
        then Alineacion := taRightJustify
        else Alineacion := taCenter;

    if gdFixed in State then begin // ¿Es una celda fija de sólo lectura?
      Brush.Color := clNavy;     // le ponemos azul de fondo
      Font.Color := clWhite      // fuente blanca
    end else begin
      if gdFocused in State then begin // ¿Esta enfocada la celda?
        if (pos('clave por si se quiere otro color el la celda enfocada', LowerCase(Grilla.Cells[3,aRow])) > 0) then
          Brush.Color := clFuchsia  // fondo violeta
        else if (pos('otra palabra clave', LowerCase(Grilla.Cells[2,aRow])) > 0) then
            Brush.Color := clGreen // fondo verde
          else Brush.Color := clRed;  // fondo rojo
        Font.Color  := clWhite;    // fuente blanca
        Font.Style  := [fsBold]    // y negrita
      end else begin // Para el resto de celdas el fondo lo ponemos blanco
        Brush.Color := clWindow;
        Font.Color  := clBlack;
        Font.Style  := []
      end
    end;
    FillRect(Rect);

    //Personalización, para el caso de modificar el ancho de las columnas, el texto no se salga
    F := DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX or DT_END_ELLIPSIS;
    if Grilla.Cells[aCol,aRow] <> '' then begin
      case Alineacion of
        taLeftJustify : F := F or DT_LEFT;
        taCenter      : F := F or DT_CENTER;
        taRightJustify: F := F or DT_RIGHT
      end;
      setbkmode(Handle, 0);
      DrawText (Handle, Grilla.Cells[ACol,ARow], -1, Rect, F)
    end
  end
end;

esto lo probé en delphi XE4

Casimiro Notevi 21-06-2014 01:03:32

Cita:

Empezado por viveba (Mensaje 478143)
Supongo ya solucionaste tu problema, para otros foristas interesados, lo siguiente puede ser una solución:

¿Y cómo ajustas el ancho?


La franja horaria es GMT +2. Ahora son las 20:56:35.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi