Recent

Author Topic: DBGrid showing (MEMO) instead desired string.  (Read 805 times)

aamaral

  • Newbie
  • Posts: 3
DBGrid showing (MEMO) instead desired string.
« on: September 08, 2021, 03:24:45 am »
I'm on this very simple piece of code but I couldn't do it work properly. I dropped into form a LibraryLoader and set the absolute path to .dll, a SQLite3Connection and set the absolute path to database, a SQLQuery, a DataSource (that points to the SQLQuery) and a DBGrid linked to that DataSource. In SQL Property of SQLQuery I have the following statement:

Code: SQL  [Select][+][-]
  1. SELECT nome AS "NOME",
  2. strftime("%d/%m/%Y", DATE(dt_apfd)) AS "APFD",
  3. strftime("%d/%m/%Y", DATE(dt_prev)) AS "PREV",
  4. numero AS "PROCESSO"
  5. FROM presos INNER JOIN processos
  6. ON processos.idproc = presos.idproc;
  7.  

For load results into DBGrid I'm using the code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   SQLite3Connection1.Open;
  4.   DBGrid1.Columns.Clear;
  5.  
  6.   SQLQuery1.Active:=True;
  7.  
  8. //SQLQuery1.FieldDefs[0].DataType:=TFieldType.ftString;
  9.   DBGrid1.Columns.Add;
  10.   DBGrid1.Columns[0].FieldName:=SQLQuery1.FieldDefs[0].Name;
  11.   DBGrid1.Columns.Add;
  12.   DBGrid1.Columns[1].FieldName:=SQLQuery1.FieldDefs[1].Name;
  13.   DBGrid1.Columns.Add;
  14.   DBGrid1.Columns[2].FieldName:=SQLQuery1.FieldDefs[2].Name;
  15.   DBGrid1.Columns.Add;
  16.   DBGrid1.Columns[3].FieldName:=SQLQuery1.FieldDefs[3].Name;
  17. end;
  18.  

The Problem: when I activete SQLQuery, DBGrid is showing (MEMO) on filds "NOME" and "PROCESSO" instead the actually string. As you can see, I tried to ``SQLQuery1.FieldDefs[0].DataType:=TFieldType.ftString;``, but it mades no difference.

I tried to change it via Properties, but actually, catch fields and its properties by activating the query has a bug wich crashes Lazarus to death, so I was trying to do it writting in program.

GAN

  • Sr. Member
  • ****
  • Posts: 338
Re: DBGrid showing (MEMO) instead desired string.
« Reply #1 on: September 08, 2021, 04:40:26 am »
Quote
The Problem: when I activete SQLQuery, DBGrid is showing (MEMO) on filds "NOME" and "PROCESSO" instead the actually string.

To solve this you have to enable dgDisplayMemoText in Option of the TDBGrid in the Object Inspector.
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

https://searchlazarus.blogspot.com/

Zvoni

  • Hero Member
  • *****
  • Posts: 734
Re: DBGrid showing (MEMO) instead desired string.
« Reply #2 on: September 08, 2021, 09:02:49 am »
Or cast it to VARCHAR in your Select-Statement
SELECT CAST(Nome As Varchar) As Nome From...."
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

aamaral

  • Newbie
  • Posts: 3
Re: DBGrid showing (MEMO) instead desired string.
« Reply #3 on: September 09, 2021, 02:16:54 am »
Thank you guys for such a quick reply. I tested both solution separately. Casting my fields in Select statement worked like a charm. Changing property dgShowMemoText in design time, it raised following exception when while running I clicked on Button1 to populate DBGrid: "External: SIGSEGV" - In address xxxxxxxxxx. But, changing same property in code, it worked perfectly.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   myDbGridOpts: TDbGridOptions;
  4. begin
  5.   SQLite3Connection1.Open;
  6.   DBGrid1.Columns.Clear;
  7.  
  8.   myDbGridOpts:=[];
  9.   Include(myDbGridOpts,dgColLines);
  10.   Include(myDbGridOpts,dgDisplayMemoText);
  11.   Include(myDbGridOpts,dgIndicator);
  12.   Include(myDbGridOpts,dgRowSelect);
  13.   Include(myDbGridOpts,dgTitles);
  14.  
  15.   SQLQuery1.Active:=True;
  16.  
  17.   DBGrid1.Columns.Add;
  18.   DBGrid1.Columns[0].FieldName:=SQLQuery1.FieldDefs[0].Name;
  19.   DBGrid1.Columns.Add;
  20.   DBGrid1.Columns[1].FieldName:=SQLQuery1.FieldDefs[1].Name;
  21.   DBGrid1.Columns.Add;
  22.   DBGrid1.Columns[2].FieldName:=SQLQuery1.FieldDefs[2].Name;
  23.   DBGrid1.Columns.Add;
  24.   DBGrid1.Columns[3].FieldName:=SQLQuery1.FieldDefs[3].Name;
  25.   DBGrid1.Options:=myDbGridOpts;
  26. end;
  27.  

 

TinyPortal © 2005-2018