Recent

Author Topic: [SOLVED] Show memo field contents (multiline text) in dbgrid  (Read 22143 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
[SOLVED] Show memo field contents (multiline text) in dbgrid
« on: August 02, 2011, 02:50:37 pm »
Hi,

I've got a dbgrid that loads memo fields (unlimited string). I've got dgEditing set to false so I only show data.
The only thing that shows up is (MEMO).
Read the grid reference wiki.

Tried to:

Two questions:
1. What do I have to do to get the data showing correctly?
2. If I want to use the code below for something else, how do I calculate BoundsRect? There is no aRow property in a dbgrid?

OnSelectEditor code:
Code: [Select]
procedure TForm1.ResultsGridSelectEditor(Sender: TObject; Column: TColumn;
  var Editor: TWinControl);
begin
  //GridCellMemo.BoundsRect:=ResultsGrid.CellRect(aCol,aRow);
  GridCellMemo.Text:=Column.Field.AsString;
  Editor:=GridCellMemo; //GridCellMemo.visible:=true, don't know if it matters
end; 
« Last Edit: March 14, 2012, 11:36:20 am 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

Lacak2

  • Guest
Re: Show memo field contents (multiline text) in dbgrid
« Reply #1 on: August 03, 2011, 12:10:27 pm »
I've got a dbgrid that loads memo fields (unlimited string). I've got dgEditing set to false so I only show data.
The only thing that shows up is (MEMO).
Yes, it is how TBlobField.GetText works (uses TField.GetClassDesc).

Two questions:
1. What do I have to do to get the data showing correctly?
I have no experience with this, but I think, that you must hook into drawing cell and draw what you want.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Show memo field contents (multiline text) in dbgrid
« Reply #2 on: August 06, 2011, 08:17:24 am »
Thanks, Lacak2, I've solved problem 2: how to show a TMemo control as an editor in a dbgrid. I've updated the wiki: http://wiki.lazarus.freepascal.org/Grids_Reference_Page#Example:_How_to_set_a_memo_editor_for_dbgrids

Comments welcome, there might be a more effective/elegant way of doing things.
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

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [Partly solved] Show memo field contents (multiline text) in dbgrid
« Reply #3 on: September 07, 2011, 01:03:12 pm »
Hmmm, I managed to get a bit further: now I can let a grid draw nothing in a memo cell - just can't it to draw the proper text.

I'm using this OnDrawColumnCell event procedure in the dbgrid to try and show memo fields:
(updated 16 September with new code)
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;


Attached:
- project source code (hint: when started, load db2secout.txt, then press export)
- screenshot of the problem.

Thanks for any help!
« Last Edit: September 16, 2011, 08:01:57 am 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

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [Still questions] Show memo field contents (multiline text) in dbgrid
« Reply #4 on: March 14, 2012, 11:36:08 am »
Got drawing to work thanks to user137.
See http://lazarus.freepascal.org/index.php/topic,16300.0.html for newest code.
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

 

TinyPortal © 2005-2018