tdbgrid_在Delphi的TDBGrid中显示和编辑MEMO字段

丰胤运
2023-12-01

tdbgrid

 If you are developing database applications with tables containing MEMO fields, you'll notice that, by default, the TDBGrid component does not show the contents of a MEMO field inside a DBGrid cell.

如果使用包含MEMO字段的表开发数据库应用程序,则会注意到,默认情况下,TDBGrid组件不会显示DBGrid单元内MEMO字段的内容。

This article provides an idea of how to solve this TMemoField's issue (with a few more tricks)...

本文提供了有关如何解决此TMemoField问题的想法(还有更多技巧)...

TMemoField ( TMemoField )

Memo fields are used to represent lengthy text or combinations of text and numbers. When building database applications using Delphi, the TMemoField object is used to represent a memo field in a dataset. TMemoField encapsulates the fundamental behavior common to fields that contain text data or arbitrary length. In most databases, the size of the Memo field is limited by the size of the database.

备注字段用于表示冗长的文本或文本和数字的组合。 使用Delphi构建数据库应用程序时,TMemoField对象用于表示数据集中的备注字段。 TMemoField封装了包含文本数据或任意长度的字段共有的基本行为。 在大多数数据库中,“备注”字段的大小受数据库大小的限制。

While you can display the contents of a MEMO field in a TDBMemo component, by design the TDBGrid will only display "(Memo)" for the contents of such fields.

虽然您可以在TDBMemo组件中显示MEMO字段的内容,但通过设计,TDBGrid将仅为此类字段的内容显示“(备注)”。

In order to actually display some text (from the MEMO field) in the appropriate DBGrid cell, you'll only need to add a simple line of code ...

为了在适当的DBGrid单元中实际显示一些文本(来自MEMO字段),您只需要添加一行简单的代码即可。

For the purpose of the next discussion, let's say you have a database table named "TestTable" with at least one MEMO field named "Data".

为了进行下一个讨论,假设您有一个名为“ TestTable”的数据库表,其中至少有一个名为“ Data”的MEMO字段。

OnGetText ( OnGetText )

To show the contents of a MEMO field in the DBGrid, you need to attach a simple line of code in the field's OnGetText event. The easiest way to create the OnGetText event handler is to use the Fields editor at design time to create a persistent field component for the memo field:

要在DBGrid中显示MEMO字段的内容,您需要在该字段的OnGetText事件中附加一行简单的代码。 创建OnGetText事件处理程序的最简单方法是在设计时使用Fields编辑器为备注字段创建一个持久性字段组件:

  1. Connect your TDataset descendant component (TTable, TQuery, TADOTable, TADOQuery ....) to the "TestTable" database table.

    将您的TDataset后代组件(TTable,TQuery,TADOTable,TADOQuery ....)连接到“ TestTable”数据库表。
  2. Double click the dataset component to open the Fields editor

    双击数据集组件以打开“字段”编辑器
  3. Add the MEMO field to the list of persistent fields

    将MEMO字段添加到持久字段列表
  4. Select the MEMO field in the Fields editor

    在“字段”编辑器中选择“ MEMO”字段
  5. Activate the Events tab in the Object Inspector

    激活对象检查器中的“事件”选项卡
  6. Double click the OnGetText event to create the event handler

    双击OnGetText事件以创建事件处理程序

Add the next line of code (italicized below):

添加下一行代码(斜体如下):


procedure TForm1.DBTableDataGetText(
Sender: TField;
var Text: String;
DisplayText: Boolean);
begin
Text := Copy(DBTableData.AsString, 1, 50);

Note: the dataset object is called "DBTable", the MEMO field is called "DATA", and therefore, by default, the TMemoField connected to the MEMO database field is called "DBTableData". By assigning DBTableData.AsString to the Text parameter of the OnGetText event, we tell Delphi to display ALL the text from the MEMO field in a DBGrid cell.You can also adapt the DisplayWidth of the memo field to a more appropriate value.

注意:数据集对象称为“ DBTable”,MEMO字段称为“ DATA”,因此,默认情况下,连接到MEMO数据库字段的TMemoField称为“ DBTableData”。 通过为OnGetText事件的Text参数分配DBTableData.AsString ,我们告诉Delphi在DBGrid单元格中显示MEMO字段中的所有文本。您还可以将备注字段的DisplayWidth调整为更合适的值。

Note: since MEMO fields can be quite BIG, it is a good idea to show only a part of it. In the above code, only the first 50 characters are displayed.

注意:由于MEMO字段可能很大,因此最好仅显示其中的一部分。 在上面的代码中,仅显示前50个字符。

在单独的表格上进行编辑 ( Editing on a separate form )

By default, the TDBGrid does not allow editing of MEMO fields. If you want to enable "in place" editing, you could add some code to react on a user action that shows a separate window that allows editing using a TMemo component.For the sake of simplicity we'll open an editing window when ENTER is pressed "on" a MEMO field in a DBGrid.Let's use the KeyDown event of a DBGrid component:

默认情况下,TDBGrid不允许编辑MEMO字段。 如果要启用“就地”编辑,则可以添加一些代码来对用户操作做出React,该操作显示一个单独的窗口,允许使用TMemo组件进行编辑。为简单起见,我们将在输入ENTER时打开一个编辑窗口。在DBGrid的MEMO字段上“按下”。让我们使用DBGrid组件的KeyDown事件:


procedure TForm1.DBGrid1KeyDown(
Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
begin
if DBGrid1.SelectedField = DBTableData then
with TMemoEditorForm.Create(nil) do
try
DBMemoEditor.Text := DBTableData.AsString;
ShowModal;
DBTable.Edit;
DBTableData.AsString := DBMemoEditor.Text;
finally
Free;
end;
end;
end;

Note 1: the "TMemoEditorForm" is a secondary form containing only one component: "DBMemoEditor" (TMemo).Note 2: the "TMemoEditorForm" was removed from the "Auto-create forms" list in the Project Options dialog window.

注意1:“ TMemoEditorForm”是仅包含一个组件的辅助表单:“ DBMemoEditor”(TMemo)。注2:“ TMemoEditorForm”已从“项目选项”对话框窗口的“自动创建表单”列表中删除。

Let's see what happens in the DBGrid1's KeyDown event handler:

让我们看看在DBGrid1的KeyDown事件处理程序中会发生什么:

  1. When a user presses the ENTER key (we are comparing the Key parameter to the VK_RETURN virtual key code) [Key = VK_RETURN],

    当用户按下ENTER键(我们将Key参数与VK_RETURN 虚拟键码进行比较 )[Key = VK_RETURN]时,

  2. If the currently selected field in the DBGrid is our MEMO field (DBGrid1.SelectedField = DBTableData),

    如果DBGrid中当前选择的字段是我们的MEMO字段(DBGrid1.SelectedField = DBTableData),
  3. We create the TMemoEditorForm [TMemoEditorForm.Create(nil)],

    我们创建TMemoEditorForm [TMemoEditorForm.Create(nil)],
  4. Send the value of the MEMO field to the TMemo component [DBMemoEditor.Text := DBTableData.AsString],

    将MEMO字段的值发送到TMemo组件[DBMemoEditor.Text:= DBTableData.AsString],
  5. Display the form modally [ShowModal],

    模态显示表单[ShowModal],
  6. When a user finishes with editing and closes the form, we need to put the dataste into the Edit mode [DBTable.Edit],

    当用户完成编辑并关闭表单时,我们需要将数据表置于“编辑”模式[DBTable.Edit],
  7. In order to be able to assign the edited value back to our MEMO field [DBTableData.AsString := DBMemoEditor.Text].

    为了能够将编辑后的值分配回我们的MEMO字段[DBTableData.AsString:= DBMemoEditor.Text]。

Note: if you are looking for more TDBGrid related articles and usage tips, be sure to visit: "TDBGrid to the MAX" tips collection.

注意:如果您正在寻找更多与TDBGrid相关的文章和使用技巧,请确保访问:“ TDBGrid to MAX ”技巧集合。

翻译自: https://www.thoughtco.com/displaying-and-editing-memo-fields-in-delphis-tdbgrid-4092538

tdbgrid

 类似资料: