Delphi DBGrid 使用 MySQL 进行格式化
我有一个关于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,我通过执行以下操作得到了正确的结果(感谢 Simon 为我指明了正确的方向):
在 ClientDataSet 之后填充后,我为 OnGetText 事件设置事件处理程序:
并将事件处理程序实现为新过程:
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:
And impliment the event handlers as new procedures:
添加断点并使用以下方法评估 (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.