DBGrid 显示“(MEMO)”作为字符串字段的值

发布于 2024-10-14 18:07:44 字数 128 浏览 11 评论 0原文

我正在尝试使用 Lazarus 和 SQLdb 组件编写一个简单的 SQLite 应用程序。

我连接到数据库并填充 TDBGrid。问题是所有文本字段列都显示值“(MEMO)”,而不是数据库中的字符串。

I'm trying to write a simple SQLite application using Lazarus and the SQLdb components.

I connect to the database and populate a TDBGrid. Problem is that all columns that are text fields display the value "(MEMO)" rather then the string in database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

乖不如嘢 2024-10-21 18:07:44

我找到了一个简单的解决方案:

必须启用 DBGrid 中的 dgDisplayMemoText 属性。

I have found a simple solution:

The property dgDisplayMemoText from the DBGrid must be enabled.

财迷小姐 2024-10-21 18:07:44

我忘记了它的来源,但这就是我对 tdbgrid 中的备注字段所做的事情。
bluish 关于 gettext 事件的说法是正确的,这是在代码中实现它的方法:

创建一个名为 MemoDifier 的类:

MemoDifier = class
  public
    procedure DBGridOnGetText(Sender: TField; var aText: string;
      DisplayText: boolean);
  end;                  

在代码的实现部分,输入以下内容:

procedure MemoDifier.DBGridOnGetText(Sender: TField; var aText: string;
  DisplayText: boolean);
begin
  if (DisplayText) then
    aText := Sender.AsString;
end; 

然后单击窗体中的 tdbgrid 控件和 Object Inspector(Lazarus IDE),单击事件选项卡,向下滚动以找到 OnPrepareCanvas 事件。双击它生成代码。然后修改代码以满足您的需要,例如 tdbgrid 控件的名称:

procedure Tmainui.TDBGrid1PrepareCanvas(sender: TObject;
  DataCol: Integer; Column: TColumn; AState: TGridDrawState);
var
  MemoFieldReveal: MemoDifier;
begin
   if (DataCol = 1) then
   begin
     try
       TDBGrid1.Columns.Items[0].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
       TDBGrid1.Columns.Items[1].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
       TDBGrid1.Columns.Items[2].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
     except
       On E: Exception do
       begin
         ShowMessage('Exception caught : ' + E.Message);
       end;
     end;
   end;
end; 

变量 MemoFieldReveal 指向类 MemoDifier。不要忘记修改索引 (Items[x]) 以指向显示 (MEMO) 文本的 tdbgrid 项目/字段的索引号。

I forgot the source of this but this is what I am doing with memo fields in tdbgrid.
bluish is right about the gettext event, this is how to implement it in the code:

Create a class called MemoDifier:

MemoDifier = class
  public
    procedure DBGridOnGetText(Sender: TField; var aText: string;
      DisplayText: boolean);
  end;                  

At the implementation section of your code, put this:

procedure MemoDifier.DBGridOnGetText(Sender: TField; var aText: string;
  DisplayText: boolean);
begin
  if (DisplayText) then
    aText := Sender.AsString;
end; 

Then click the tdbgrid control in your form and at the Object Inspector(Lazarus IDE), click the Events tab, scroll below to find the OnPrepareCanvas event. Double click it to generate the code. Then modify the code to suit to your needs such as the name of your tdbgrid control:

procedure Tmainui.TDBGrid1PrepareCanvas(sender: TObject;
  DataCol: Integer; Column: TColumn; AState: TGridDrawState);
var
  MemoFieldReveal: MemoDifier;
begin
   if (DataCol = 1) then
   begin
     try
       TDBGrid1.Columns.Items[0].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
       TDBGrid1.Columns.Items[1].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
       TDBGrid1.Columns.Items[2].Field.OnGetText := @MemoFieldReveal.DBGridOnGetText;
     except
       On E: Exception do
       begin
         ShowMessage('Exception caught : ' + E.Message);
       end;
     end;
   end;
end; 

The variable MemoFieldReveal points to the class MemoDifier. Don't forget to modify the index (Items[x]) to point to your index number of the tdbgrid items/fields which shows the (MEMO) text.

番薯 2024-10-21 18:07:44

备注字段无法显示在 TDBGrid 中。将 TDBMemo 添加到表单并将其连接到相同的 TDataSource。在这种情况下,您将在备忘录中看到文本。

Memo fields cannot be shown in the TDBGrid. Add TDBMemo to the form and connect it to the same TDataSource. You will see the text in your memo in this case.

感性不性感 2024-10-21 18:07:44

老问题,但我在寻找类似问题时遇到了它,但是是通过代码而不是使用 DBGrid 检索的数据;当我使用 DisplayTextText 方法检索字符串字段时,我得到了“(memo)”而不是正确的值。

答案其实很简单……TSQLQuery 类拥有一组名为 AsXxx 的方法来根据数据类型获取数据。这里使用 AsString 来内联分配一个变量。

SQLQuery1.Open;
//some check to make sure there are fields
name:=SQLQuery1.Fields[1].AsString;   //gives "English" for example
code:=SQLQuery1.Fields[2].DisplayText;   //gives "(Memo) instead of "en"

此外,如果您在设计时不知道变量的类型(例如,您想为任何类型的表设计通用函数),那么 TSqlQuery 对象的 FieldDefs 属性可以为您提供帮助。
它拥有一个 DataType 属性,允许选择要使用的 AsXxx 函数。这里以为例。

SQLQuery1.Open;
//some check to make sure there are fields
with SQLQuery1.FieldDefs[1].DataType of
  ftMemo: SQLQuery1.Fields[1].DisplayText;        //gives "(Memo)"
  ftString: name:=SQLQuery1.Fields[1].AsString;  //gives "English" for example
...
end;

如果您在调试时查看数据类型,您会注意到,使用 SQLite,任何文本都被视为 ftMemo,而不是 ftString,因为那里只有一种类型的文本。

Old question but I came across it while looking for a similar issue, but on the data retrieved by code rather than with a DBGrid; When I retrieved a string fields with the methods DisplayText or Text, I got "(memo)" instead of the correct value.

The answer is actually simple… The TSQLQuery class owns a set of methods called AsXxx to get the data according to a type of data. Here use AsString to assign a variable inline.

SQLQuery1.Open;
//some check to make sure there are fields
name:=SQLQuery1.Fields[1].AsString;   //gives "English" for example
code:=SQLQuery1.Fields[2].DisplayText;   //gives "(Memo) instead of "en"

More, if you don't know at design time of which type the variable is (for example, you want to design a generical function for any kind of table) with its FieldDefs property of your TSqlQuery object can help you.
It owns a DataType property that allows to choose which AsXxx function to use. Here for example.

SQLQuery1.Open;
//some check to make sure there are fields
with SQLQuery1.FieldDefs[1].DataType of
  ftMemo: SQLQuery1.Fields[1].DisplayText;        //gives "(Memo)"
  ftString: name:=SQLQuery1.Fields[1].AsString;  //gives "English" for example
...
end;

And if you look at the datatype while debugging, you will notice that with SQLite, any text is seen as a ftMemo, not a ftString for there is only one type of text there.

幸福还没到 2024-10-21 18:07:44

正如 IRC 上所述,您可能需要将查询字段添加到表单中(以便为它们生成“字段”组件),然后实现 TMemoField.GetText 事件。

看看在对象检查器中输入“字段”字段是否会弹出一个编辑器来生成组件(在 Zeos iirc 中是这样做的)。

As said on IRC, you probably need to add the fields of your query to the form, (so that "field" components are generated for them) and then implement the TMemoField.GetText event.

See if entering the "fields" field in the object inspector brings up an editor to generate the components (it does so in Zeos iirc).

策马西风 2024-10-21 18:07:44

我在 MySQL 和 Tgrid 中有相同的内容,所以我希望答案是相同的,因为它非常简单:-)

说如果 s 是导致问题的字段,那么不要写

SELECT s

write

SELECT LEFT(s,200) AS s 

I have the same in MySQL and Tgrid so I'm hoping the answer is the same as it is quite simple :-)

Say if s is the field causing the problem then Instead of writing

SELECT s

write

SELECT LEFT(s,200) AS s 
前事休说 2024-10-21 18:07:44

一个看似简单的解决方案是在列类型中使用类似 VARCHAR(n) 的内容来限制字段中 TEXT 的长度,其中 n 是允许的最大字符数。

An apparently simple solution is to limit the length of the TEXT in the field using something like VARCHAR(n) in the column type where n is the maximum number of allowed characters.

花之痕靓丽 2024-10-21 18:07:44

另一种选择

如果您使用 TZConection,请在连接数据库时将此行添加到您的代码中

TZConnection).Properties.Add('Undefined_Varchar_AsString_Length=100'); 

Another option

If you are using TZConection add this line to you code when you Connect you database

TZConnection).Properties.Add('Undefined_Varchar_AsString_Length=100'); 
很酷不放纵 2024-10-21 18:07:44

经过一些测试,我可以看到虽然字段被视为“MEMO”,但对于“string”字段,在创建表时只需保留“VARCHAR”选项而不是“TEXT”即可。

after some tests, I could see that although the fields are treated as "MEMO", for "string" fields, just keep the "VARCHAR" option instead of "TEXT" when creating the table.

无力看清 2024-10-21 18:07:44

您还可以在 sql 中简单地使用 TRIM:

SELECT TRIM(myfield) as NewField

这个技巧也适用于 Lazarus。

You can also simple use TRIM in your sql:

SELECT TRIM(myfield) as NewField

This trick also works in Lazarus.

南烟 2024-10-21 18:07:44

如果是oracle数据库,更改Tquery并放入选择字符串....cast(field as varchar2(100) as field....

If oracle database, change Tquery and put on select string .... cast(field as varchar2(100) as field ....

寻找一个思念的角度 2024-10-21 18:07:44

本文给出了解决方案:在Delphi的TDBGrid中显示和编辑MEMO字段

在这里,我总结了您必须执行的操作:

  • 在 .dfm 中,将 OnGetText = MyDataSetMyFieldGetText 添加到属于您的数据的 TMemoField(此处名为 MyField) 集合(例如 TTable,此处名为 MyDataSet
  • .pas 中的

    界面 > 类型>在表单定义中,添加

    procedure MyDataSetMyFieldGetText(Sender: TField; var Text: string; DisplayText: Boolean);
    
  • 在 .pas > 中添加

    实施 >添加这个方法

    过程 TDM.WorkVisiteNoteGetText(Sender: TField; var Text: string; DisplayText: Boolean);
    开始
      文本 := 复制(WorkVisiteNote.AsString, 1, 100);
    结尾;
    

This article gives a solution: Displaying and editing MEMO fiels in Delphi's TDBGrid.

Here I summarize what you have to do:

  • in the .dfm add OnGetText = MyDataSetMyFieldGetText to the TMemoField (here named MyField) belonging to your data set (for example a TTable, here named MyDataSet)
  • in the .pas > interface > type > inside your form definition, add

    procedure MyDataSetMyFieldGetText(Sender: TField; var Text: string; DisplayText: Boolean);
    
  • in the .pas > implementation > add this method

    procedure TDM.WorkVisiteNoteGetText(Sender: TField; var Text: string; DisplayText: Boolean);
    begin
      Text := Copy(WorkVisiteNote.AsString, 1, 100);
    end;
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文