Delphi DBGrid 使用 MySQL 进行格式化

发布于 2024-12-14 09:29:29 字数 564 浏览 3 评论 0原文

我有一个关于 delphi DBGrid 中数据单元格格式的问题。 DBGrid 组件连接到 MySQL 数据库,该数据库在运行时进行填充。

我有一个用于日期时间的列和一个用于布尔值的列。当日期时间列的时间部分为0时,它只显示日期,但我需要它显示日期和时间,即使时间为零。布尔字段显示 1 或 0,但我需要它显示“开”或“关”。

我尝试过转换字段,然后设置类似于

(ClientDataSet2.FieldByName('Timestamp') as TDateTimeField).DisplayFormat := 'yyyy/mm/dd hh:mm:ss';

和 的

(ClientDataSet2.FieldByName('Value') as TBooleanField).DisplayValues := 'On;Off'; 

格式,但收到一条错误消息:“Exception class EInvalidCast with message 'Invalid class typecast'”。

对此的任何帮助将不胜感激。

I have a question regarding formatting of data cells in the delphi DBGrid. The DBGrid component is connected to a MySQL database, which gets populated at run time.

I have a column for DateTime and one for Boolean. When the time part of the datetime column is 0, it only displays the date, but I need it to display the date and time, even though the time is zero. The boolean field displays 1 or 0, but i need it to display "on" or "off".

I have tried casting the fields, and then setting the formatting like

(ClientDataSet2.FieldByName('Timestamp') as TDateTimeField).DisplayFormat := 'yyyy/mm/dd hh:mm:ss';

and

(ClientDataSet2.FieldByName('Value') as TBooleanField).DisplayValues := 'On;Off'; 

but I get an error saying: "Exception class EInvalidCast with message 'Invalid class typecast'."

Any help with this will be most appreciated.

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

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

发布评论

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

评论(2

錯遇了你 2024-12-21 09:29:29

因此,我通过执行以下操作得到了正确的结果(感谢 Simon 为我指明了正确的方向):

在 ClientDataSet 之后填充后,我为 OnGetText 事件设置事件处理程序:

ClientDataSet2.FieldByName('TimeStamp').OnGetText := TimeStampGetText;
ClientDataSet2.FieldByName('Value').OnGetText := ValueGetText;

并将事件处理程序实现为新过程:

procedure TTimelineForm.ValueGetText( Sender : TField; var Text : string; DisplayText : Boolean );
begin
    if Sender.AsInteger = 0 then
        Text := 'OFF'
    else
        Text := 'ON';
end;

procedure TTimelineForm.TimeStampGetText( Sender : TField; var Text : string; DisplayText : Boolean );
    var
        DateTime : TDateTime;
    begin
        Text := FormatDateTime( 'yyyy/mm/dd hh:mm:ss', Sender.AsDateTime );
    end;

So I got it right by doing the following (Thanks to Simon for pointing me in the right direction):

Right after the ClientDataSet is populated, I set the event handlers for the OnGetText events:

ClientDataSet2.FieldByName('TimeStamp').OnGetText := TimeStampGetText;
ClientDataSet2.FieldByName('Value').OnGetText := ValueGetText;

And impliment the event handlers as new procedures:

procedure TTimelineForm.ValueGetText( Sender : TField; var Text : string; DisplayText : Boolean );
begin
    if Sender.AsInteger = 0 then
        Text := 'OFF'
    else
        Text := 'ON';
end;

procedure TTimelineForm.TimeStampGetText( Sender : TField; var Text : string; DisplayText : Boolean );
    var
        DateTime : TDateTime;
    begin
        Text := FormatDateTime( 'yyyy/mm/dd hh:mm:ss', Sender.AsDateTime );
    end;
情归归情 2024-12-21 09:29:29

添加断点并使用以下方法评估 (Ctrl+F7) 正确的类名: ClientDataSet2.FieldByName('Value').ClassName

并将无效的类名替换为适当的类名。

Add a breakpoint and evaluate (Ctrl+F7) the correct classname with: ClientDataSet2.FieldByName('Value').ClassName

And replace the invalid classnames with the appropiate classnames.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文