Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-09

Drawing memo text in DBGrid - tried but stuck

2 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

Hi all,

I'm trying to draw text from DBF memo fields on a dbgrid instead of the standard "(Memo)".
The memo fields contain text data that is probably not that long (about a couple of thousand characters max), which I'd like to display in a read-only dbgrid.

I've come up with some code that:
1. Still correctly displays an integer field
2. Does not seem to write anything in memo cells (cells are blank)

I suppose I have to manually refresh the grid contents somehow because .DefaultDrawing is false? But why are the integer fields then displayed correctly? So it must be something in my code below, I suppose.

Thanks for any hints.
The complete project can be found at
https://bitbucket.org/reiniero/db2securityscript/downloads (to run it, please import the file db2secout.txt that can be found on that download page as well)

The code I have now is:
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 Fnil 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;

Reply by email

Reinier Olislagers
to Reinier Olislagers
Reinier Olislagers wrote 21 lines
Hi all, I'm trying to draw text from DBF memo fields on a dbgrid instead of the standard "(Memo)". The memo fields contain text data that is probably not that long (about a couple of thousand characters max), which I'd like to display in a read-only dbgrid. I've come up with some code that: 1. Still correctly displays an integer field 2. Does not seem to write anything in memo cells (cells are blank) I suppose I have to manually refresh the grid contents somehow because .DefaultDrawing is false? But why are the integer fields then displayed correctly? So it must be something in my code below, I suppose. Thanks for any hints. The complete project can be found at https://bitbucket.org/reiniero/db2securityscript/downloads (to run it, please import the file db2secout.txt that can be found on that download page as well)

Bump... any suggestions? Probably something quite simple.

Current code is still:
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 Fnil 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;

Reply by email