Multiline text on dbGrid lazarus / 31-LCL ---------------------------------------------------------------------- NotNickolay · 2017-01-27 14:22 Hi Folks, I've create a small application that reads strings from a text field of mysql, so this string can return 1 char to N chars (talking about the text length), after some searches on Google, I finally found a way, that makes the dbGrid show the right text instead of a (memo) advice, but the code doesn't create the LineEndings based on the cell width. So my question is, how can I update the cell heigth of the cells (or lines if you prefer) of my grid, so when I have a big string this line can be higher, and stay with the default size when is a small string ---------------------------------------------------------------------- msch · 2022-01-03 00:09 Well as long as we can not override the procedure "DefaultDrawCell" of the TCustomGrid, there is no clean way to build a Lazarus DBGrid- Component with MultiLine Components inserted. ---------------------------------------------------------------------- wp · 2022-01-03 00:42 The LCL grids are much superior to Delphi's grids. For specific painting requirements it usually is enough to write a handler for the OnPrepareCanvas event instead of redrawing the entire cell. OnPrepareCell is called immediately before the cell is painted and you have a last chance to modify drawing parameters, for example switching the Canvas.TextStyle to multiline mode: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;  Column: TColumn; AState: TGridDrawState);var  ts: TTextStyle;begin  if Column.Field.DataType = ftMemo then  begin    ts := DBGrid1.Canvas.TextStyle;    ts.SingleLine := false;    ts.Wordbreak := true;    DBGrid1.Canvas.TextStyle := ts;  end;end; ---------------------------------------------------------------------- msch · 2022-01-04 21:35 Hi thanks for your code snipped.   Let's make a minimal component and look whats gonna happen Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit UNIT_DB_TSDBGrid; interfaceuses  DBGrids, DB, DBCtrls,  Windows, Messages, SysUtils, Classes, CommDlg, ShellAPI, LCLType,         ShlObj, Registry, ComCtrls, Controls, StdCtrls, ExtCtrls, Grids, Graphics,  Menus, Dialogs;type   TSDBGrid = class(TDBGrid)  private      constructor Create(AOwner: TComponent);        destructor Destroy;  public        procedure DoPrepareCanvas( aCol, aRow: Integer; aState: TGridDrawState);  end; implementation constructor TSDBGrid.Create(AOwner: TComponent);begin  inherited Create(AOwner);end; destructor TSDBGrid.Destroy;begin   inherited Destroy;end; procedure TSDBGrid.DoPrepareCanvas(aCol, aRow: Integer; aState: TGridDrawState);var  DataCol    : Integer;  IsSelected : boolean;  ts         : TTextStyle;begin  { non fixed Rows}  if (ARow>=FixedRows) then  begin    if not DefaultDrawing then begin    GetSelectedState(aState, IsSelected);      if IsSelected then begin      Canvas.Brush.Color := SelectedColor;        Canvas.Font.Color := clHighlightText;      end;    end;    if Assigned(OnPrepareCanvas) then begin      DataCol := ColumnIndexFromGridColumn(aCol);      if DataCol>=0 then        OnPrepareCanvas(Self, DataCol, TColumn(Columns[DataCol]), aState);    end;    if DataCol>=0 then    //    if Columns[DataCol].Field.DataType = ftMemo then   // in comment for demonstration      begin        ts := Canvas.TextStyle;        ts.SingleLine := false;        ts.Wordbreak := true;        Canvas.TextStyle := ts;      end;  end;end;  First the code snipped works - trés chic - Now whats about the "ftmemo" - have a look into the attchment. ---------------------------------------------------------------------- wp · 2022-01-04 22:13 You must provide a handler for the memo field's OnGetText event. Here you can pass the text to be displayed by the grid: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MemoGetTextHandler(Sender: TField; var aText: string;  DisplayText: Boolean);begin  if DisplayText and (Sender.DataType = ftMemo) then    aText := Sender.AsString;end; Unless you work with persistent fields (where you can assign the handler in the object inspector) you must search the memo field(s) and assign the event handler manually: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.QryArticlesAfterOpen(DataSet: TDataSet);var  f: TField;begin  for f in Dataset.Fields do    if f.DataType = ftMemo then      f.OnGetText := @MemoGetTextHandler;end; ---------------------------------------------------------------------- egsuh · 2022-01-05 03:46 Hi wp, Is it possible to extend display lines --- if two lines, the hight of that row is adjusted to show two lines ---- within OnPrepareCanvas? I did this in different way, but if there are simpler ways, I'd like to use it. ---------------------------------------------------------------------- egsuh · 2022-01-05 08:12 QuoteIs it possible to extend display lines --- if two lines, the hight of that row is adjusted to show two lines ---- within OnPrepareCanvas? I did this in different way, but if there are simpler ways, I'd like to use it. Forget this. I'm using this already^^ ---------------------------------------------------------------------- msch · 2022-01-05 11:20 I see: To use the  handler for the memo field's OnGetText event is an elgant solution for the Form. You run the code once your're open the query. The advantage seems to me that you may have several grids and no one has the [memo] Display-Text. Well I hesitate to use this in a derived component due to the possibility to have more then one Grid on the form. In this case every Grid would have it's handler code and maybe one Grid should have the [memo] displayed and the other one not. In this szenario one Grid would decide for all other grids too. ---------------------------------------------------------------------- wp · 2022-01-05 18:14 I forgot, there's an even simpler possibility: Check the grid option dgDisplayMemoText to show the memo as full text rather than '(Memo)' only. ---------------------------------------------------------------------- msch · 2022-01-05 22:19 The orignal Lazarus DBGrid shows with dgDisplayMemoText the memo text - but without carriage returns in the result - simply a long line.  So the combination of the snipped and the option made the run for the DBGrid with MultiLine Elements in the Cell. Currently there are some Grid-Components for Delphi with Multiline but for Lazarus I found none. Cool     Thanks a lot !