zoukankan      html  css  js  c++  java
  • 关于TField.OnGetText事件(200分)

    怎么这个事件象只对Integer和String类型的字段起作用
    其它的象TDateTime或Currency,就算你对该字段设置了这个事件
    也触发不了

    但我现在要在DBGrid里显示一个日期值,常常会出现1899-12-30的
    我现在想格式化这个输入,该如何做。

    (所使用的Query是动态创建的)


    在系统主窗口(主程序)创建事件(ONCREATE)事件中,给下面两个变量赋值就可以了: 
    ShortDateFormat:='YYYY-MM-DD';//这里可以设置你需要的格式 
    DateSeparator:='-';


    修改TField的DisplayFormat属性

    你可以这样的吧?(OnGetText)
    if ((Sender as TField).AsString='1900-1-1') then Text:=''
    else Text:=(Sender as TField).AsString;

    你也可以在DBGrid的OnDrawColumnCell中处理与显示相关的问题....
    ...
    if (Column.Field.DataType=ftDate) or 
    (Column.Field.DataType=ftDateTime) 
    then
    if Column.Field.AsString='1899-12-30' then
    DBGrid1.Canvas.FillRect(Rect)
    else DBGrid1.DefaultDrawColumnCell...
    end
    else DBGrid1.DefaultDrawColumnCell...

    谢谢大家
    现在我的问题不是不知道怎么格式化日期值
    而是当在数据库没有这个日期值时
    则这个日期值会可能是0或0.5XXXX的
    所以会在GRID里显示1889-XX-XX的无意义数值
    我现在想当日期值小于一定值时不显示出来

    本来在TField.onGetText可以做这种工作的
    可惜当TField的类型为TDateTime或某些类型时
    就算设置了其onGetText事件,但也不会触发

    先用varisnull判断一下
    if varisnull(((Sender as TField).value)) then
    text:='字段为空'

    简单得很,就这点代码示例:
    procedure TForm1.ClientDataSet1CDSDesigner4GetText(Sender: TField;
    var Text: String; DisplayText: Boolean);
    begin
    if ((Sender as TField).AsDateTime<StrToDate('1999-1-1')) then Text:=''
    else Text:=(Sender as TField).AsString
    end;
    日期在1999-1-1前的均显示为空,注意是固定字段...

    我是这样的,用一个类来封装了一些对数据库的操作、
    在类里面建立一个 Query 来存取数据
    以下是一些实现代码 


    type
    TUsersInfo = Class(TComponent)
    private
    ...
    procedure StatusGetText(Sender: TField; var Text: string; DisplayText: Boolean);
    procedure DateGetText(Sender: TField; var Text: string; DisplayText: Boolean);

    public
    ... ...
    property DataSet : TDBISAMQuery Read FQuery;
    property DataSource : TDataSource read FDataSource write SetDataSource;

    procedure ReadCurUserInfo; 

    published
    ... ...
    end;

    //---------------------------------

    procedure TUsersInfo.ReadCurUserInfo;
    begin
    with FQuery do
    begin
    if not Active then Open;

    ... ...

    FieldByName('Status').OnGetText := StatusGetText;

    FieldByName('JoinDate').OnGetText := DateGetText;

    FieldByName('Status').Alignment := taLeftJustify;
    end;
    ... ...
    end;

    procedure TUsersInfo.StatusGetText(Sender: TField; var Text: string;
    DisplayText: Boolean);
    begin
    Text := GetUserStatusNameByID(Sender.AsInteger);
    end;


    procedure TUsersInfo.DateGetText(Sender: TField; var Text: string;
    DisplayText: Boolean);
    begin
    if Sender.AsDateTime > 100 then //这里只是一个约数,在100内的日期值对实际来说应该是没有意思的
    Text := FormatDateTime('yyyy-mm-dd', Sender.AsDateTime)
    else
    Text := '';
    end;


    在界面上用一个TDBGrid 联到 TUsersInfo.DataSource
    可是从 TDBGrid来看也好,设断点也好
    StatusGetText() 能触发,而DateGetText()就不会触发

    Why ??

    "JoinDate"字段是一个TDateTime 类型的,而且还发现对Currency等也不行,
    就String 或Integer 可以

    问题就是这样呵
    代码应该没有问题吧

    字段的创建,我根本没有创建呵

    FieldByName('Status').OnGetText := StatusGetText;
    FieldByName('JoinDate').OnGetText := DateGetText;

    对于两个差不多是一样的赋值,为什么一个OK一个却不会执行到呀

    如果把上面的代码改改

    procedure TUsersInfo.ReadCurUserInfo;
    begin
    with FQuery do
    begin
    if not Active then Open;

    ... ...

    FieldByName('Status').OnGetText := StatusGetText;

    FieldByName('Status').OnGetText := DateGetText;
    end;
    ... ...
    end;

    这样DateGetText()事件又会触发,只不过在执行到时出现转换错误而已

    在系统主窗口(主程序)创建事件(ONCREATE)事件中,给下面两个变量赋值就可以了: 
    ShortDateFormat:='YYYY-MM-DD';//这里可以设置你需要的格式 
    DateSeparator:='-';

    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    Vuecli3项目引入网页视频流媒体播放器EasyPlayer.JS报videojs not definde错误如何解决?
    穷人和富人的区别
    如何使用PCATTCP测试局域网传输速度
    DirectFB同时显示到X11和VNC上
    使用Openssl生成CA及签发证书方法
    内存泄漏定位
    关于在C++开发的项目中引用后缀名为.c文件的一句话提醒
    在Win32下建立GTK开发环境
    GTK/DFB中的WaitCursor
    GTK+/DFB优化
  • 原文地址:https://www.cnblogs.com/jijm123/p/13943915.html
Copyright © 2011-2022 走看看