Arsip: DateTimePicker di dalam DBGrid


by adludin in Articles more 15 years ago 2375
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..
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com