* * *

Author Topic: [SOLVED] Showing complete memo text in dbgrid?  (Read 6999 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
[SOLVED] Showing complete memo text in dbgrid?
« on: March 11, 2012, 02:29:06 pm »
Current Lazarus, FPC fixes 2.6
Asked this before but got no further, so trying again...

I've got a dbgrid with memo fields (from a DBF file). In many cases, those memo fields contain not that much text at all.
By default, I think a memo field shows up as (Memo) in a dbgrid, which is a bit... useless.

I would like to
  • show the full text of the cells. If the text is too big, it can be truncated
  • optionally: in the selected cell, show scrollbars that allow the user to view the entire cell text

Having read the grids wiki page (but without Delphi experience) I've cobbled together this code, which doesn't work - it doesn't seem to paint correctly:
Code: [Select]
procedure TForm1.ResultsGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
// Draw memo text instead of (Memo)
// Note: grid DefaultDrawing property must be off to avoid duplicate drawing
// To get this, I basically copied the existing DefaultDrawColumnCell procedure
// but tested for memo first. If no memo, pass on to default procedure.
// Maybe slower, more complicated, but it allows for changes in the
// core Lazarus DefaultDrawColumnCell procedure.
var
  OverrideDraw: boolean; //determine if we're going to override drawing
  S: string;
  F: TField;
  DataRow: Integer;
begin
  OverrideDraw:=false;
  try
    F := Column.Field;
    if F.DataType = ftMemo then
    begin
      OverRideDraw:=true;
    end;
  except
    on E: exception do
    begin
      // We might have an inactive datalink or whatever,
      // in that case, pass on our problems to the existing
      // procedure.
      //showMessage('Exception: ' + E.Classname + '/' + E.Message);
      OverRideDraw:=false;
    end;
  end;

  if OverRideDraw=false then
  begin
    // Call normal procedure to handle drawing for us.
    ResultsGrid.DefaultDrawColumnCell(Rect,DataCol,Column,State);
  end
  else
  begin
    // Get to work displaying our memo contents
    // Basically shamelessly ripped from
    // DefaultDrawColumnCell
    // maybe fix something for first/header row
    if F<>nil then
    begin
      //DO display memo ;)
      S := F.AsString; //DisplayText will only show (Memo)
    end
    else
    begin
      S := '';
    end;
    //Actual drawing, taken from Grids.DrawCellText coding:
    ResultsGrid.Canvas.TextRect(Rect,Left,Top, S);
  end;
end;

Help welcome.

Project source code can be found under the outputparser directory in https://bitbucket.org/reiniero/db2securityscript/src  (You can download the code as a zip, too)

Thanks!
« Last Edit: March 11, 2012, 05:19:20 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

User137

  • Hero Member
  • *****
  • Posts: 1688
    • Nxpascal home
Re: Showing complete memo text in dbgrid?
« Reply #1 on: March 11, 2012, 03:57:27 pm »
Code: [Select]
procedure TForm1.ResultsGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
...
    ResultsGrid.Canvas.TextRect(Rect,Left,Top, S);
...
Are you sure that's right parameters? Left and Top properties are the coordinates of the Form1, not specific cell you are propably trying to draw. Rect.Left and Rect.Top may give the position where cell is.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Showing complete memo text in dbgrid?
« Reply #2 on: March 11, 2012, 05:19:08 pm »
Thanks User137, no I'm not sure at all, I was basically copy-pasting everything I could find...

I tried it and it works - thanks a lot!

Now I just need to redraw the grid when some event (OnDraw or whatever) fires -  when I display a messagebox, the grid doesn't redraw...
Back to the wiki grids page - but at least I've got the right text now.

Thanks again!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

christensen

  • Full Member
  • ***
  • Posts: 124
Re: [SOLVED] Showing complete memo text in dbgrid?
« Reply #3 on: September 06, 2016, 09:06:48 pm »
Hi BigChimp,

Could you please give detail how do you solve.
I have a few dbf tables and some of them contain memo field, usually the last column and i want to display it in TDBGrid.


Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

engkin

  • Hero Member
  • *****
  • Posts: 1557
Re: [SOLVED] Showing complete memo text in dbgrid?
« Reply #4 on: September 06, 2016, 09:15:15 pm »
 :(

LacaK

  • Hero Member
  • *****
  • Posts: 537

christensen

  • Full Member
  • ***
  • Posts: 124
Re: [SOLVED] Showing complete memo text in dbgrid?
« Reply #6 on: September 07, 2016, 08:25:37 am »
Thanks,

I've tried like on the first link, but it returns me empty cell. When i click on the cell it return (MEMO)

« Last Edit: September 07, 2016, 08:42:13 am by christensen »
Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

gduck

  • Newbie
  • Posts: 1
Re: [SOLVED] Showing complete memo text in dbgrid?
« Reply #7 on: March 29, 2017, 11:08:30 am »
Change last line!

ResultsGrid.Canvas.TextRect(Rect,Rect.Left,Rect.Top, S);

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus