tags:

views:

276

answers:

4

I have a table with a RTF field in it.

How can I display this in a TDBGrid so that it actually shows the rich text, and not just (FMTMEMO) in the column?

I really don't want to use a third party component for this.

Thanks!

+2  A: 

Delphi has no built-in component that does that.

There are plenty external components that can do it.

A simple google query will list enough of them.

--jeroen

Jeroen Pluimers
A: 

If your just wanting the text in the column, then the easiest way would be to add a calculated field to your dataset of type String to a reasonable length (say 80) and in the onCalculate event for the dataset pull the plain text from the RTF field into the string field. Use the string field for display rather than the RTF field.

If your wanting all of the formatting, then this gets a bit trickier as there is no default component support for rendering RTF data other than the TRichEdit and descendants which are wrappers around a Microsoft control. You will have to do a lot of the RTF parsing yourself, and use the OnDrawDataCell event to paint the text into the cell. If you take this approach, then the RTF specifications document will be of great help.

skamradt
+2  A: 

I've done a very crude example for you that works, which you can then take on to try and improve as you need.

Drop a TDBRichEdit control onto your form and set its Visible property to False. Set the DataSource and DataField properties to pick up the appropriate field.

Say the field name that holds the RTF text is called "RTF":

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  DrawState: Integer;
  DrawRect: TRect;
begin
  if (gdFocused in State) then
  begin
    if (Column.Field.FieldName = 'RTF') then
    with DBRichEdit1 do
    begin
      Left := Rect.Left + DBGrid1.Left + 1;
      Top := Rect.Top + DBGrid1.Top + 1;

      Visible := True;
    end;
  end;
end;

procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
  if DBGrid1.SelectedField.FieldName = 'RTF' then
   DBRichEdit1.Visible := False;
end;

This will show the full, formatted richedit text in a popup window when you click on the column in the grid. When you click away, it hides the popup window.

_J_
+4  A: 

I don't know if this is transferrable to your situation, but I once used OwnerDrawing to get rich text. If this is an option check out DrawRtfText in unit DrawRichText.

Ulrich Gerhardt
I haven't used that code, but I have used the "windowless rich edit control" that it's based on. I ended up writing a TRtfLabel control just like it, along with a standalone PaintRTF function, and I was pleased with the results. It's the perfect solution for owner-draw situations.
Rob Kennedy