我想在DBGRID中显示如下:

想象一下“网格”,如下所示:

ID-描述

1-网格的第1行
2-网格的第2行
3-网格的第3行

现在,假设DESCRIPTION列的大小已更改,并且不再显示单词“ GRID”;

我想保持良好的DBGRID

ID-描述

1-第1行

2-第2行

3-第3行

有没有可能??

最佳答案

不是您的要求,但可能会有所帮助...我曾经使用以下代码在标准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;


对于Blob /备注字段,您可能还会发现执行一些自定义GetText在网格中直接显示某些内容很有用:

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;


这就是结果的样子。
delphi - 具有行高变量的DBGRID-LMLPHP

PS:对不起,非标准代码样式。

关于delphi - 具有行高变量的DBGRID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32256591/

10-10 23:16