Tip dan trik untuk menyisipkan komponen DateTimePicker ke dalam DBGrid untuk field bertipe Date/DateTime
Source code :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, ComCtrls, Grids, DBGrids, ExtCtrls, DBCtrls;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DateTimePicker1: TDateTimePicker;
Table1: TTable;
Table1OrderNo: TFloatField;
Table1CustNo: TFloatField;
Table1SaleDate: TDateTimeField;
Table1ShipDate: TDateTimeField;
Table1EmpNo: TIntegerField;
Table1ShipToContact: TStringField;
Table1ShipToAddr1: TStringField;
Table1ShipToAddr2: TStringField;
Table1ShipToCity: TStringField;
Table1ShipToState: TStringField;
Table1ShipToZip: TStringField;
Table1ShipToCountry: TStringField;
Table1ShipToPhone: TStringField;
Table1ShipVIA: TStringField;
Table1PO: TStringField;
Table1Terms: TStringField;
Table1PaymentMethod: TStringField;
Table1ItemsTotal: TCurrencyField;
Table1TaxRate: TFloatField;
Table1Freight: TCurrencyField;
Table1AmountPaid: TCurrencyField;
DataSource1: TDataSource;
DBNavigator1: TDBNavigator;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
procedure DateTimePicker1Change(Sender: TObject);
procedure DateTimePicker1DropDown(Sender: TObject);
private
{ Private declarations }
FActiveField: TField;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
FActiveField:= Column.Field;
If Column.Field.DataType In [ftDate, ftDateTime] Then
with DateTimePicker1 do
begin
Left := Rect.Left + DBGrid1.Left + 1;
Top := Rect.Top + DBGrid1.Top + 1;
Width := Rect.Right - Rect.Left + 2;
Width := Rect.Right - Rect.Left + 2;
Height := Rect.Bottom - Rect.Top + 2;
DateTime:= Column.Field.AsDateTime;
Visible := True;
end;
end
end;
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
If FActiveField.DataType In [ftDate, ftDateTime] Then
DateTimePicker1.Visible := False
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key = Chr(9)) then Exit;
DBGrid1.DataSource.Edit;
If FActiveField.DataType In [ftDate, ftDateTime] Then
begin
DateTimePicker1.SetFocus;
SendMessage(DateTimePicker1.Handle, WM_Char, word(Key), 0);
end;
end;
procedure TForm1.DateTimePicker1Change(Sender: TObject);
begin
if DBGrid1.DataSource.State in [dsEdit, dsInsert] then
FActiveField.Value := DateTimePicker1.DateTime;
end;
procedure TForm1.DateTimePicker1DropDown(Sender: TObject);
begin
DBGrid1.DataSource.Edit;
end;
end.
Catatan :
Dengan prinsip yang sama, bisa juga diimplementasikan untuk kontrol-kontrol yang lain, seperti TSpinEdit untuk field bertipe integer..Random Articles
Last Articles
Recent Topic
- PascalTalk #6: (Podcast) Kuliah IT di luar negeri, susah gak sih?
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #5: UX: Research, Design and Engineer
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #4: Obrolan Ringan Seputar IT
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #2: Membuat Sendiri SMART HOME
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #3: RADically Fast and Easy Mobile Apps Development with Delphi
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #1: Pemanfaatan Artificial Intelligence di Masa Covid-19
by LuriDarmawan in Tutorial & Community Project more 5 years ago - Tempat Latihan Posting
by LuriDarmawan in OOT more 5 years ago - Archive
- Looping lagi...
by idhiel in Hal umum tentang Pascal Indonesia more 13 years ago - [ask] koneksi ke ODBC user Dsn saat runtime dengan ado
by halimanh in FireBird more 13 years ago - Validasi menggunakan data tanggal
by mas_kofa in Hal umum tentang Pascal Indonesia more 13 years ago