DBGrid中插入DateTimePicker
procedure TForm13.DateTimePicker1Change(Sender: TObject); //写回 日期 begin DBGrid1.DataSource.Edit; DBGrid1.Columns[2].Field.Value := DateTimePicker1.DateTime; //2 为 'DateAdded'所在的列序号 fdquery1.Post; end; procedure TForm13.DateTimePicker1KeyPress(Sender: TObject; var Key: Char); begin if (key = Chr(13)) then begin //手动 在 DateTimePicker1输入日期数字后,按回车 保存到 数据库 DateTimePicker1.Visible:= false; DBGrid1.DataSource.Edit; DBGrid1.Columns[2].Field.Value := DateTimePicker1.DateTime; fdquery1.Post; end; end; procedure TForm13.DBGrid1ColExit(Sender: TObject);// 退出 时,不显示 编辑 控件 begin if DBGrid1.SelectedField.FieldName = 'password' then DateTimePicker1.Visible := False; end; procedure TForm13.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if (gdFocused in State) then begin if (Column.Field.FieldName = 'password') 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; DateTimePicker1.Date:= Column.Field.Value;// 数据库 传给 DateTimePicker1 Visible := True; end; end end; procedure TForm13.DBGrid1KeyPress(Sender: TObject; var Key: Char); begin if (key = Chr(9)) then Exit; //TAB键 直接 退出 if (DBGrid1.SelectedField.FieldName = 'password') then begin DateTimePicker1.SetFocus; SendMessage(DateTimePicker1.Handle, WM_Char, word(Key), 0); end end; procedure TForm13.FormCreate(Sender: TObject); begin FormatSettings.DateSeparator := '-'; FormatSettings.TimeSeparator := ':'; FormatSettings.ShortDateFormat := 'yyyy-MM-dd'; FormatSettings.LongDateFormat := 'yyyy-MM-dd'; FormatSettings.TimeAMString := ''; FormatSettings.TimePMString := ''; FormatSettings.ShortTimeFormat := 'hh:nn:ss'; FormatSettings.LongTimeFormat := 'hh:nn:ss:zzz'; end;