2

I would like to show in a DBGRID as follows:

Imagine "Grid" as follows:

ID - DESCRIPTION

1 - Line 1 of the grid
2 - Line 2 of the grid
3 - Line 3 of the grid

Now, suppose the size of the DESCRIPTION column is changed and no longer appear the words "GRID";

I would like to stay as well DBGRID

ID - DESCRIPTION

1 - Line 1 of the
     grid
2 - Line 2 of the
     grid
3 - Line 3 of the
     grid
is there any possibility that ??

7
  • So what you're asking for is word-wrapping cells in a db grid? Are you wanting different rows to be different heights automatically? Or set one single property for all rows? Commented Aug 27, 2015 at 18:15
  • i need rows with different heights automatically.. whit this [link] (community.devexpress.com/blogs/thinking/VCL/Grid/…) Commented Aug 27, 2015 at 18:25
  • That's not something that can be applied directly to a DB Grid. You'll either have to get that library or one like it, or roll out your own custom grid control. I suggest though not to even use a DB Grid - Data aware controls, while easy to make stuff work, makes it more difficult in areas like this - as well as work in an outdated manner. You're better off just using a standard grid which can have variable row sizes - and pump your data into it manually (which it appears you'll have a lot of customization to do in this sense anyway). Commented Aug 27, 2015 at 18:31
  • This example is merely illustrative. What I really need is a line break and to show that text. [link] officeblogswest.blob.core.windows.net/wp-content/… but i'll try with stringgrid component ... Commented Aug 27, 2015 at 18:39
  • Fwiw, the devexpress tcxGrid can be used in bound (DB-aware) and unbound modes. Its main downside (apart from the size of the devex VCL library) is cost. Commented Aug 27, 2015 at 18:43

1 Answer 1

4

Not what you're asking, but might help... I once used this code to show complete Memo fields in the standard DBGrid:

  TMyForm = class(TForm)
    ...
  private
    FormMemoRect: TRect;
    MemoGrid: TDBGrid;
    BMemo: TBitBtn;
    ...

Procedure TMyForm.FormMemoDeactivate(Sender: TObject);
Begin
    (Sender As TForm).Close;
    Sender.Free;
End;

Procedure TMyForm.BMemoClick(Sender: TObject);
Var FormMemo: TForm;
Begin
    MemoGrid.SetFocus;

    FormMemo := TForm.Create(Self);
    With TMemo.Create(FormMemo) Do Begin
        Parent := FormMemo;
        Align := alClient;
        ReadOnly := True;
        WordWrap := True;
        ScrollBars := ssVertical;
        Lines.Text := MemoGrid.DataSource.DataSet.Fields[TComponent(Sender).Tag].AsString;
    End;

    With FormMemo Do Begin
        OnDeactivate := FormMemoDeactivate;
        Left := FormMemoRect.Left;
        Top := FormMemoRect.Top;
        Width := Max(FormMemoRect.Right - FormMemoRect.Left, 300);
        Height := FormMemoRect.Bottom - FormMemoRect.Top;
        BorderStyle := bsNone;

        Show;
    End;
End;

Procedure TMyForm.GrdMemoDrawColumnCell(Sender: TObject; Const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
Begin
    If (gdFocused In State) Then Begin
        If Column.Field.DataType In [ftBlob, ftMemo] Then Begin
            {Desenha botão para visualização do Memo}
            FormMemoRect.Left := TWinControl(Sender).ClientToScreen(Rect.TopLeft).X;
            FormMemoRect.Right := TWinControl(Sender).ClientToScreen(Rect.BottomRight).X;
            FormMemoRect.Top := TWinControl(Sender).ClientToScreen(Rect.BottomRight).Y;
            FormMemoRect.Bottom := FormMemoRect.Top + 100;

            If Not Assigned(BMemo) Then
                BMemo := TBitBtn.Create(Self);
            BMemo.Parent := TWinControl(Sender).Parent;
            BMemo.Width := 16;
            BMemo.Height := 16;
            BMemo.Caption := '...';
            BMemo.OnClick := BMemoClick;
            BMemo.Tag := Column.Field.Index;
            BMemo.Left := TWinControl(Sender).Left + Rect.Right - BMemo.Width + 1;
            BMemo.Top := TWinControl(Sender).Top + Rect.Top + 2;

            MemoGrid := TDBGrid(Sender);
        End
        Else
            FreeAndNil(BMemo);
    End;
End;

For Blob/Memo Fields, you may also find it useful to do some custom GetText to show something directly in the Grid:

Procedure TMyForm.DataSetMemoGetText(Sender: TField; var Text: String; DisplayText: Boolean);
Begin
    Text := Copy(Sender.AsString, 1, 50);

    If Text <> Sender.AsString Then
        Text := Text + '...';
End;

This is how the result looks like. Memo on Grid

PS: Sorry for non-standard code style.

Sign up to request clarification or add additional context in comments.

1 Comment

Over eight years after you shared, I came across your answer and implemented this in a project that I started today. It works great. I'm using Delphi 11.3 and InfoPower TwwDBGrid and the only difference is the event for hooking the button (OnDrawDataCell). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.