Board index » delphi » Displaying Memo field contents in a Grid?, How to

Displaying Memo field contents in a Grid?, How to

Please help.

I cant find in the help or the documentation how to get a memo field to
display in a DBGrid component.  All of the other fields display fine when the
table is set active or the App is run.  How do you get the contents of the
databases Memo field to show?

 

Re:Displaying Memo field contents in a Grid?, How to


taybr...@xmission.com (Taylor H. Brady) wrote:

Quote
>Please help.

>I cant find in the help or the documentation how to get a memo field to
>display in a DBGrid component.  All of the other fields display fine when the
>table is set active or the App is run.  How do you get the contents of the
>databases Memo field to show?

I recently asked the same question and this is one of several replys I
got.

This is from Borland:

Q: "How do I show the contents of a memo field in a DBGrid?"

A:  Use the following code for the OnDrawDataCell event of the DBGrid.
Note:
    before running create a TMemoField object for the memo field by
double
    clicking on the TTable component and adding the memo field.

    procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect:
TRect;
      Field: TField; State: TGridDrawState);
    var
      P  : array [0..50] of char;   {array size is number of
characters needed}
      BS : tBlobStream;             {from the memo field}
      S  : String;
    begin
      If Field is TMemoField then begin
      with (Sender as TDBGrid).Canvas do
        begin
          {Table1Notes is the TMemoField}
          BS := tBlobStream.Create(Table1Notes, bmRead);
          FillChar(P,SizeOf(P),#0);               {terminate the null
string}
          BS.Read(P, 50);           {read 50 chars from memo into
blobStream}
          BS.Free;
          S := StrPas(P);
          while Pos(#13, S) > 0 do              {remove carriage
returns and}
            S[Pos(#13, S)] := ' ';              {line feeds}
          While Pos(#10, S) > 0 do
            S[Pos(#10, S)] := ' ';
          FillRect(Rect);                          {clear the cell}
          TextOut(Rect.Left, Rect.Top, S);         {fill cell with
memo data}
        end;
      end;
    end;

Scott Paterson
Computer Services Dept.
City Hospital NHS Trust, Bham Uk.

Re:Displaying Memo field contents in a Grid?, How to


taybr...@xmission.com (Taylor H. Brady) wrote:

Quote
>Please help.
>I cant find in the help or the documentation how to get a memo field to
>display in a DBGrid component.  All of the other fields display fine when the
>table is set active or the App is run.  How do you get the contents of the
>databases Memo field to show?

Taylor,

I have found a way to display the first part of the memo text by
attaching the following code to the Memo Field's (In my case it's a
field called Diary Note) OnGetText event.

procedure TMemoGrdFm.DiaryTableDiaryNoteGetText(Sender: TField;
  var Text: OpenString; DisplayText: Boolean);
var
   MemoLines:    TStringList;
begin
if not(Sender.isNull) then
   begin
      try
        { Create a string list for the memo }
        MemoLines := TStringList.Create;
        MemoLines.Assign(Sender);
        if (MemoLines.Count > 0) then
           Text := Copy(MemoLines.Strings[0],1,Sender.DisplayWidth);
      finally
        MemoLines.Free;
      end;
   end;
end;

This will allow the user to see some of what is in the memo but to
edit it they will still have to use a TDBMemo.
If anyone has better ideas I'd like to see them.

Hope this helps.

David

Other Threads