我的TDBGrid正在反转我的日期,即使它是一个字符串 我在我的数据库上有这个STRING
01/06/2016 09:05
我只是希望它显示为字符串,而不是日期
当我将它设置为TDBGrid时,结果是:
09:05 01/06/2016
我的数据库是FoxPro .DBF,我要求的数据是 STRING ,不是日期
代码为
procedure TfrmMensagensRecEnv.dbgRecebidasDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var sData, sHora, sTotal : String;
begin
sHora := Copy(MSGTBL.FieldByName('DATA_LIDA').AsString,12,20); (09:50:55)
sData := Copy(MSGTBL.FieldByName('DATA_LIDA').AsString,0,10); (01/06/2016)
if Column.FieldName = 'DATA_LIDA' then
begin
sTotal := sData + ' ' + sHora;
dbgRecebidas.Canvas.TextRect(Rect,Rect.Left+3,Rect.Top, sTotal);
end;
end;
我使用SQL assistent im上传TinyPic中的图片,使用显示我的字符串数据和网格。
http://tinypic.com/r/2jameco/9
这是我的.DFM网格代码
object dbgRecebidas: TDBGrid
Left = 2
Top = 2
Width = 774
Height = 197
DataSource = dtsMSGTBL
DrawingStyle = gdsClassic
FixedColor = 16771022
GradientEndColor = 16771022
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgCancelOnExit, dgTitleClick]
ParentFont = False
TabOrder = 0
TitleFont.Charset = ANSI_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnCellClick = dbgRecebidasCellClick
OnDrawColumnCell = dbgRecebidasDrawColumnCell
OnDblClick = dbgRecebidasDblClick
OnKeyPress = dbgRecebidasKeyPress
Columns = <
item
Color = 15461355
Expanded = False
FieldName = 'COD_MSG'
Title.Caption = 'Cod Msg'
Title.Font.Charset = ANSI_CHARSET
Title.Font.Color = clWindowText
Title.Font.Height = -11
Title.Font.Name = 'MS Sans Serif'
Title.Font.Style = [fsBold]
Width = 60
Visible = True
end
item
Color = 15461355
Expanded = False
FieldName = 'COD_TIP'
Title.Caption = 'Tipo'
Title.Font.Charset = ANSI_CHARSET
Title.Font.Color = clWindowText
Title.Font.Height = -11
Title.Font.Name = 'MS Sans Serif'
Title.Font.Style = [fsBold]
Width = 56
Visible = True
end
item
Color = 15461355
Expanded = False
FieldName = 'COD_USU'
Title.Caption = 'Usu'#225'rio'
Title.Font.Charset = ANSI_CHARSET
Title.Font.Color = clWindowText
Title.Font.Height = -11
Title.Font.Name = 'MS Sans Serif'
Title.Font.Style = [fsBold]
Visible = True
end
item
Color = 15461355
Expanded = False
FieldName = 'TITULO'
Title.Caption = 'T'#237'tulo'
Title.Font.Charset = ANSI_CHARSET
Title.Font.Color = clWindowText
Title.Font.Height = -11
Title.Font.Name = 'MS Sans Serif'
Title.Font.Style = [fsBold]
Width = 322
Visible = True
end
item
Color = 15461355
Expanded = False
FieldName = 'DATA_INCL'
Title.Caption = 'Data de Inclus'#227'o'
Title.Font.Charset = ANSI_CHARSET
Title.Font.Color = clWindowText
Title.Font.Height = -11
Title.Font.Name = 'MS Sans Serif'
Title.Font.Style = [fsBold]
Width = 101
Visible = True
end
item
Color = 15461355
Expanded = False
FieldName = 'DATA_LIDA'
Title.Caption = 'Data de Leitura'
Title.Font.Charset = ANSI_CHARSET
Title.Font.Color = clWindowText
Title.Font.Height = -11
Title.Font.Name = 'MS Sans Serif'
Title.Font.Style = [fsBold]
Width = 93
Visible = True
end
答案 0 :(得分:1)
以下是不的完全独立项目的代码 展示你描述的问题。结果显示为
因此,如果您的项目获得了不同的结果,那么这是由您未在代码或DFM中显示的内容引起的。
如您所见,日期+时间显示的唯一区别是,对于TDateTime字段,包含秒数。
顺便说一下,我希望能够像Tom Brunberg建议的那样,展示准备MCVE的价值。
代码:
TForm1 = class(TForm)
CDS1: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
procedure FormCreate(Sender: TObject);
public
end;
[...]
procedure TForm1.FormCreate(Sender: TObject);
var
Field : TField;
begin
Field:= TIntegerField.Create(Self);
Field.FieldName := 'ID';
Field.Name := Field.FieldName;
Field.FieldKind := fkData;
Field.DataSet := CDS1;
Field:= TStringField.Create(Self);
Field.Size := 40;
Field.FieldName := 'StringField';
Field.Name := Field.FieldName;
Field.FieldKind := fkData;
Field.DataSet := CDS1;
Field:= TDateTimeField.Create(Self);
Field.FieldName := 'DateTimeField';
Field.Name := Field.FieldName;
Field.FieldKind := fkData;
Field.DataSet := CDS1;
CDS1.CreateDataSet;
CDS1.InsertRecord([1, '01/06/2016 09:05', '01/06/2016 09:05']);
end;