Mega Code Archive

 
Categories / Delphi / ADO Database
 

How do I show the contents of a memo field in a DBGrid

Title: How do I show the contents of a memo field in a DBGrid? Question: By default the DBGrid Control of the VCL can't display memo fields. Here is a solution... Answer: The problem can be solved with the OnDrawDataCell event of the DBGrid. 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} hStr: 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; hStr := StrPas(P); while Pos(#13, hStr) 0 do {remove carriage returns and} hStr[Pos(#13, hStr)] := ' '; while Pos(#10, hStr) 0 do {line feeds} S[Pos(#10, hStr)] := ' '; FillRect(Rect); {clear the cell} TextOut(Rect.Left, Rect.Top, hStr); {fill cell with memo data} end; end; end; Note: before running create a TMemoField object for the memo field by double clicking on the TTable component and adding the memo field.