Mega Code Archive

 
Categories / Delphi / VCL
 

Multi Line cells in Stringgrid

Title: Multi-Line cells in Stringgrid Question: How to draw more than one line inside a cell of TStringGrid ? Answer: First, you need to set the grid's DefaultDrawing property to False. Next, you should use the following logic as your OnDrawCell event handler for the grid. procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint; Rect: TRect; State: TGridDrawState); var Line1: string; Line2: string; ptr: integer; padding: integer; hGrid: TStringGrid; begin hGrid:= (Sender as TStringGrid); ptr := Pos(';', hGrid.Cells[Col, Row]); if ptr 0 then begin Line1 := Copy(hGrid.Cells[Col, Row], 1, ptr - 1); Line2 := Copy(hGrid.Cells[Col, Row], ptr + 1, Length(hGrid1.Cells[Col,Row]) - ptr); end else Line1 := hGrid.Cells[Col, Row]; hGrid.Canvas.FillRect(Rect); hGrid.Canvas.TextOut(Rect.Left, Rect.Top + 2, Line1); if ptr 0 then hGrid.Canvas.TextOut(Rect.Left, Rect.Top - hGrid.Canvas.Font.Height + 3, Line2); end; Now you can embed a semi-colon to denote a line break in the column heading. Also remember to make row zero deep enough to accommodate the multiple lines, for example, by including the following statement in your OnCreate event handler for the form: StringGrid1.RowHeights[0] := StringGrid1.DefaultRowHeight * 2 ;