Grid with multiline cells  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 24/11/99

Tip accessed 753 times

 


  • Put a TStringGrid (StringGrid1) in your form
  • Put this code in the OnDrawCell of StringGrid1:


     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;
    




    and for add a multiline cell, separate each line with #13, for example:


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





    Second method



    This second example use the API function DrawText:


     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;
    



    You can put this in the OnCreate event of the form to see the effect:


     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;