You've already forked lazarus-ccr
Added: ftDateTime support to TJDBGridControl
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2020 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -5,6 +5,7 @@ Note: Lazarus Trunk required
|
|||||||
|
|
||||||
Version pre-1.0
|
Version pre-1.0
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
2011-09-27 Added: ftDateTime support to TJDBGridControl
|
||||||
2011-09-26 Added: TJDateTimeEdit, TJLabeledDateTimeEdit
|
2011-09-26 Added: TJDateTimeEdit, TJLabeledDateTimeEdit
|
||||||
TJDBDateTimeEdit, TJDBLabeledDateTimeEdit
|
TJDBDateTimeEdit, TJDBLabeledDateTimeEdit
|
||||||
2011-09-23 Added: TJDBLabeledEdit, TJTimeEdit, TJLabeledTimeEdit,
|
2011-09-23 Added: TJDBLabeledEdit, TJTimeEdit, TJLabeledTimeEdit,
|
||||||
|
@ -19,6 +19,7 @@ type
|
|||||||
timeDbGridControl: TJDbGridTimeCtrl;
|
timeDbGridControl: TJDbGridTimeCtrl;
|
||||||
integerDbGridControl: TJDbGridIntegerCtrl;
|
integerDbGridControl: TJDbGridIntegerCtrl;
|
||||||
doubleDbGridControl: TJDbGridDoubleCtrl;
|
doubleDbGridControl: TJDbGridDoubleCtrl;
|
||||||
|
dateTimeDbGridControl: TJDbGridDateTimeCtrl;
|
||||||
protected
|
protected
|
||||||
{ Protected declarations }
|
{ Protected declarations }
|
||||||
//procedure SelectEditor; override;
|
//procedure SelectEditor; override;
|
||||||
@ -76,6 +77,7 @@ begin
|
|||||||
ftSmallint, ftInteger: Result := integerDbGridControl.Editor(Self);
|
ftSmallint, ftInteger: Result := integerDbGridControl.Editor(Self);
|
||||||
ftDate: Result := dateDbGridControl.Editor(Self);
|
ftDate: Result := dateDbGridControl.Editor(Self);
|
||||||
ftTime: Result := timeDbGridControl.Editor(Self);
|
ftTime: Result := timeDbGridControl.Editor(Self);
|
||||||
|
ftDateTime: Result := dateTimeDbGridControl.Editor(Self);
|
||||||
ftCurrency, ftFloat, ftBCD: Result := doubleDbGridControl.Editor(Self);
|
ftCurrency, ftFloat, ftBCD: Result := doubleDbGridControl.Editor(Self);
|
||||||
// TODO: ftDateTime. strings?
|
// TODO: ftDateTime. strings?
|
||||||
end;
|
end;
|
||||||
@ -85,7 +87,7 @@ end;
|
|||||||
procedure TJDBGridControl.UpdateData;
|
procedure TJDBGridControl.UpdateData;
|
||||||
begin
|
begin
|
||||||
if not (SelectedField.DataType in [ftSmallInt, ftInteger, ftDate,
|
if not (SelectedField.DataType in [ftSmallInt, ftInteger, ftDate,
|
||||||
ftTime, ftCurrency, ftFloat, ftBCD]) then
|
ftTime, ftDateTime, ftCurrency, ftFloat, ftBCD]) then
|
||||||
inherited UpdateData;
|
inherited UpdateData;
|
||||||
// TODO... think more about this
|
// TODO... think more about this
|
||||||
end;
|
end;
|
||||||
@ -97,6 +99,7 @@ begin
|
|||||||
timeDbGridControl := TJDbGridTimeCtrl.Create;
|
timeDbGridControl := TJDbGridTimeCtrl.Create;
|
||||||
integerDbGridControl := TJDbGridIntegerCtrl.Create;
|
integerDbGridControl := TJDbGridIntegerCtrl.Create;
|
||||||
doubleDbGridControl := TJDbGridDoubleCtrl.Create;
|
doubleDbGridControl := TJDbGridDoubleCtrl.Create;
|
||||||
|
dateTimeDbGridControl := TJDbGridDateTimeCtrl.Create;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TJDBGridControl.Destroy;
|
destructor TJDBGridControl.Destroy;
|
||||||
@ -105,6 +108,7 @@ begin
|
|||||||
timeDbGridControl.Free;
|
timeDbGridControl.Free;
|
||||||
integerDbGridControl.Free;
|
integerDbGridControl.Free;
|
||||||
doubleDbGridControl.Free;
|
doubleDbGridControl.Free;
|
||||||
|
dateTimeDbGridControl.Free;
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -27,6 +27,31 @@ uses
|
|||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
|
{ TJDbGridDateTimeCtrl }
|
||||||
|
|
||||||
|
TJDbGridDateTimeCtrl = class(TObject)
|
||||||
|
private
|
||||||
|
Field: TField;
|
||||||
|
updated: boolean;
|
||||||
|
theValue: TDateTime;
|
||||||
|
fFormat: string;
|
||||||
|
function getFormat: string;
|
||||||
|
procedure myEditEnter(Sender: TObject);
|
||||||
|
procedure myEditOnEditingDone(Sender: TObject);
|
||||||
|
procedure formatInput;
|
||||||
|
procedure setFormat(const AValue: string);
|
||||||
|
procedure OnKeyPress(Sender: TObject; var key: char);
|
||||||
|
procedure OnKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
|
||||||
|
public
|
||||||
|
CellEditor: TStringCellEditor;
|
||||||
|
theGrid: TDBGrid;
|
||||||
|
function isNull: boolean;
|
||||||
|
property format: string read getFormat write setFormat;
|
||||||
|
constructor Create;
|
||||||
|
destructor Destroy; override;
|
||||||
|
function Editor(aGrid: TDBGrid): TStringCellEditor;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TJDbGridTimeCtrl }
|
{ TJDbGridTimeCtrl }
|
||||||
|
|
||||||
TJDbGridTimeCtrl = class(TObject)
|
TJDbGridTimeCtrl = class(TObject)
|
||||||
@ -128,6 +153,143 @@ implementation
|
|||||||
uses
|
uses
|
||||||
Math;
|
Math;
|
||||||
|
|
||||||
|
{ TJDbGridDateTimeCtrl }
|
||||||
|
|
||||||
|
function TJDbGridDateTimeCtrl.getFormat: string;
|
||||||
|
begin
|
||||||
|
Result := fFormat;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TJDbGridDateTimeCtrl.myEditEnter(Sender: TObject);
|
||||||
|
begin
|
||||||
|
Field := theGrid.SelectedField;
|
||||||
|
CellEditor.BoundsRect := theGrid.SelectedFieldRect;
|
||||||
|
CellEditor.Text := Field.AsString;
|
||||||
|
CellEditor.OnKeyPress := @OnKeyPress; // Recuperamos el control :-p
|
||||||
|
CellEditor.OnKeyDown := @OnKeyDown;
|
||||||
|
theValue := Field.AsDateTime;
|
||||||
|
updated := False;
|
||||||
|
CellEditor.SelectAll;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TJDbGridDateTimeCtrl.myEditOnEditingDone(Sender: TObject);
|
||||||
|
begin
|
||||||
|
CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue);
|
||||||
|
if Length(CellEditor.Caption) = 0 then
|
||||||
|
begin
|
||||||
|
Field.DataSet.Edit;
|
||||||
|
Field.Value := Null;
|
||||||
|
theValue := 0;
|
||||||
|
updated := True;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if IsValidDateString(CellEditor.Caption) then
|
||||||
|
begin
|
||||||
|
if (not updated) then
|
||||||
|
begin
|
||||||
|
theValue := StrToDate(CellEditor.Caption);
|
||||||
|
Field.DataSet.Edit;
|
||||||
|
Field.AsDateTime := theValue;
|
||||||
|
end;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
ShowMessage(CellEditor.Caption + ' no es una fecha válida');
|
||||||
|
CellEditor.Text := FormatDateTime(format, theValue);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TJDbGridDateTimeCtrl.formatInput;
|
||||||
|
begin
|
||||||
|
if theValue <> 0 then
|
||||||
|
CellEditor.Caption := FormatDateTime(format, theValue);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TJDbGridDateTimeCtrl.setFormat(const AValue: string);
|
||||||
|
begin
|
||||||
|
fFormat := AValue;
|
||||||
|
formatInput;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TJDbGridDateTimeCtrl.OnKeyPress(Sender: TObject; var key: char);
|
||||||
|
begin
|
||||||
|
if not (Key in ['0'..'9', #8, #9, '.', '-', '/', ':', ' ']) then
|
||||||
|
Key := #0;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TJDbGridDateTimeCtrl.OnKeyDown(Sender: TObject; var Key: word;
|
||||||
|
Shift: TShiftState);
|
||||||
|
begin
|
||||||
|
if Length(CellEditor.Caption) <> 0 then
|
||||||
|
if (Key in [VK_RETURN, VK_TAB, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT]) and
|
||||||
|
(not IsValidDateTimeString(NormalizeDateTime(CellEditor.Caption, theValue))) then
|
||||||
|
begin
|
||||||
|
ShowMessage(CellEditor.Caption + ' no es una fecha válida');
|
||||||
|
CellEditor.Text := FormatDateTime(format, theValue);
|
||||||
|
CellEditor.SelectAll;
|
||||||
|
Key := VK_UNKNOWN;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if key = VK_ESCAPE then
|
||||||
|
begin
|
||||||
|
if Field.IsNull then
|
||||||
|
CellEditor.Text := ''
|
||||||
|
else
|
||||||
|
CellEditor.Text := FormatDateTime(format, Field.AsDateTime);
|
||||||
|
updated := True;
|
||||||
|
theGrid.SetFocus; // No perder el foco
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if Key in [VK_UP, VK_DOWN] then
|
||||||
|
begin
|
||||||
|
Key := VK_UNKNOWN;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if Key in [VK_RETURN, VK_TAB, VK_RIGHT, VK_LEFT] then
|
||||||
|
begin
|
||||||
|
CellEditor.Caption := NormalizeDateTime(CellEditor.Caption, theValue);
|
||||||
|
if Length(CellEditor.Caption) = 0 then
|
||||||
|
theValue := 0
|
||||||
|
else
|
||||||
|
if IsValidDateTimeString(CellEditor.Caption) then
|
||||||
|
begin
|
||||||
|
theValue := StrToDateTime(CellEditor.Caption);
|
||||||
|
Field.DataSet.Edit;
|
||||||
|
Field.AsDateTime := theValue;
|
||||||
|
CellEditor.SelectAll;
|
||||||
|
updated := True;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TJDbGridDateTimeCtrl.isNull: boolean;
|
||||||
|
begin
|
||||||
|
Result := theValue = 0;
|
||||||
|
end;
|
||||||
|
|
||||||
|
constructor TJDbGridDateTimeCtrl.Create;
|
||||||
|
begin
|
||||||
|
inherited Create;
|
||||||
|
CellEditor := TStringCellEditor.Create(nil);
|
||||||
|
CellEditor.OnEnter := @myEditEnter;
|
||||||
|
CellEditor.OnKeyDown := @OnKeyDown;
|
||||||
|
CellEditor.OnEditingDone := @myEditOnEditingDone;
|
||||||
|
CellEditor.OnKeyPress := @OnKeyPress;
|
||||||
|
format := ShortDateFormat + ' ' + ShortTimeFormat;
|
||||||
|
end;
|
||||||
|
|
||||||
|
destructor TJDbGridDateTimeCtrl.Destroy;
|
||||||
|
begin
|
||||||
|
CellEditor.Free;
|
||||||
|
inherited Destroy;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TJDbGridDateTimeCtrl.Editor(aGrid: TDBGrid): TStringCellEditor;
|
||||||
|
begin
|
||||||
|
theGrid := aGrid;
|
||||||
|
Result := CellEditor;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TJDbGridTimeCtrl }
|
{ TJDbGridTimeCtrl }
|
||||||
|
|
||||||
function TJDbGridTimeCtrl.getFormat: string;
|
function TJDbGridTimeCtrl.getFormat: string;
|
||||||
|
Reference in New Issue
Block a user