Lazarus: DBGrid showing "(MEMO)" as the value of string fields in SQLite 3

HandyGandy picture HandyGandy · Jan 26, 2011 · Viewed 15.3k times · Source

I'm trying to write a simple SQLite 3 application using Lazarus and the SQLdb components.

I have managed to connect to the database and populate a TDBGrid. Problem is that all the columns that are text fields display the value "(MEMO)" rather then the string in the DB.

Answer

Allan Registos picture Allan Registos · Dec 9, 2014

I forgot the source of this but this is what I am doing with memo fields in tdbgrid. bluish is right about the gettext event, this is how to implement it in the code:

Create a class called MemoDifier:

MemoDifier = class
  public
    procedure DBGridOnGetText(Sender: TField; var aText: string;
      DisplayText: boolean);
  end;                  

At the implementation section of your code, put this:

procedure MemoDifier.DBGridOnGetText(Sender: TField; var aText: string;
  DisplayText: boolean);
begin
  if (DisplayText) then
    aText := Sender.AsString;
end; 

Then click the tdbgrid control in your form and at the Object Inspector(Lazarus IDE), click the Events tab, scroll below to find the OnPrepareCanvas event. Double click it to generate the code. Then modify the code to suit to your needs such as the name of your tdbgrid control:

procedure Tmainui.TDBGrid1PrepareCanvas(sender: TObject;
  DataCol: Integer; Column: TColumn; AState: TGridDrawState);
var
  MemoFieldReveal: MemoDifier;
begin
   if (DataCol = 1) then
   begin
     try
       TDBGrid1.Columns.Items[0].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
       TDBGrid1.Columns.Items[1].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
       TDBGrid1.Columns.Items[2].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
     except
       On E: Exception do
       begin
         ShowMessage('Exception caught : ' + E.Message);
       end;
     end;
   end;
end; 

The variable MemoFieldReveal points to the class MemoDifier. Don't forget to modify the index (Items[x]) to point to your index number of the tdbgrid items/fields which shows the (MEMO) text.