Grid con celdas de varias lineas  

Enviado Por: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Fecha: 24/11/99

Truco accedido 132 veces

 


  • Pon un TStringGrid (StringGrid1) en tu form
  • Pon este código en el evento OnDrawCell del StringGrid:


     procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
       Rect: TRect; State: TGridDrawState);
     var
       Texto    :string;
       Indice   : integer;
       Posicion : integer;
     begin
       if Pos(#13,StringGrid1.Cells[Col,Row])<>0 then
       begin
         Texto:=StringGrid1.Cells[Col,Row]+#13;
         StringGrid1.Canvas.FillRect(Rect);
         Indice:=0;
         repeat
           Posicion:=Pos(#13,Texto);
    
           with StringGrid1.Canvas do
             TextOut( Rect.left,
                      Rect.Top+(Indice*TextHeight(Copy(Texto,1,Posicion-1))),
                      Copy(Texto,1,Posicion-1));
           Inc(Indice);
           Delete(Texto,1,Posicion);
         until Posicion=0;
       end;
     end;
    




    Y para añadir una celda con varias lineas, separa cada linea por #13, por ejemplo:


      StringGrid1.Cells[2,2]:='Una linea'#13'dos lineas'#13'tres lineas';
    





    Segundo método



    Este segundo ejemplo usa la función del API DrawText, que es mucho más potente:


     procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
       Rect: TRect; State: TGridDrawState);
    
     var
       Grid    : TStringGrid;
       Texto   : String;
     begin
    
       Grid := TStringGrid(Sender);
    
       if (Row < Grid.FixedRows) or (Col < Grid.FixedCols)
       then
         Grid.Canvas.Brush.Color := clBtnFace
       else
         Grid.Canvas.Brush.Color := clWhite;
    
       Grid.Canvas.FillRect(Rect);
       Texto := Grid.Cells[Col,Row];
       DrawText( Grid.Canvas.Handle,
                 PChar(Texto),
                 StrLen(PChar(Texto)),
                 Rect,
                 DT_WORDBREAK);
    
     end;
    



    Puedes poner esto en el OnCreate de tu form, para ver lo que ocurre:


     procedure TForm1.FormCreate(Sender: TObject);
     begin
       StringGrid1.Cells[2,1] := 'Ejemplo de celdas multilineas'+#13+
                                 'Example of multiline cells';
       StringGrid1.Options:= StringGrid1.Options+[goRowSizing,goColSizing];
     end;