DateTimePicker in DBGrid query lazarus / 16-Databases ---------------------------------------------------------------------- daveinhull · 2018-12-04 00:04 Hi all, I'm trying to put a DateTimePicker in a DBGrid so I can get the date into the grid. I'm using DBGrid1DrawColumnCell to trap the cell where the date is needed using this code which positions the DateTimePicker control over the cell. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);begin  if (gdFocused in State) then  begin    if (Column.Field.FieldName = 'FuelDate') then    with DateTimePicker1 do    begin      if SQLQuery1.State = dsInsert then        DBGrid1.SelectedField.Value := Now ;      Date := DBGrid1.SelectedField.AsDateTime;      Left := Rect.Left + DBGrid1.Left + 1;      Top := Rect.Top + DBGrid1.Top + 1;      Width := Rect.Right - Rect.Left + 1;      Height := Rect.Bottom - Rect.Top + 1;      Visible := True;      DateTimePicker1.SetFocus;    SendMessage(DateTimePicker1.Handle, WM_Char, Word(32), 0);      DBGrid1.DataSource.Edit;    end;  endend; Then when the user hits tab I want it to return to the Grid and move to the next cell as if the DateTimePicker control wasn't there.So I use this code to trap the tab key, return focus to the grid and then send the DBgrid the tab key so that it moves to the next cell. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DateTimePicker1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin  if Key = 09 then then  begin    DBGrid1.SetFocus;    SendMessage(DBGrid1.Handle, WM_Char, Key, 0);  end ;end; I also have an ColExit routine which updates the cell with the date from the DateTimePicker which works. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DBGrid1ColExit(Sender: TObject);begin  if DBGrid1.SelectedField.FieldName = 'FuelDate' then  begin    if DBGrid1.DataSource.State in [dsEdit, dsInsert] then      DBGrid1.SelectedField.value := DateTimePicker1.Date;    DateTimePicker1.Visible := False  end;end; However, it doesn't work, the tab does set the DBGrid to focus, as the DateTimePicker loses it, althogh I suspect that it is just because the tab is moving to the next control anyway. But, the SendMessage doesn't seem to do anything, I still need to press tab again to move to the next cell. One thought I had was that you cannot do things like setfocus and sendmessage in an OnDo routine? Can anyone give me some help or point me to an example or documentation around this? Many thanks ave ---------------------------------------------------------------------- daveinhull · 2018-12-04 00:27 Hi, Ok, so just after posting I found the documentaiton on SendMessage and realised that I should be using KeyDown rather than Char, and now it works, but if anyone can offer any neater code, please let me know. Thanks ---------------------------------------------------------------------- eldonfsr · 2019-09-01 16:08 Hi i tried to use your code but send me error SendMessage indentifier not found what unit i have use to get thats works. ---------------------------------------------------------------------- wp · 2019-09-01 21:21 Quote from: daveinhull on December 04, 2018, 01:04:34 amI'm using DBGrid1DrawColumnCell to trap the cell where the date is needed using this code which positions the DateTimePicker control over the cell. This is wrong. The OnDrawXXX events are for drawing, nothing else. You even call SetFocus which may trigger another drawing cycle and lead to an unfinite loop. For introducing special editors there is the OnSelectEditor event. Did you try this one? And for ending the editing process you should use OnEditingDone where you can write the date to the database and the hide the DateTimePicker. Look at the demo in the examples folder of the Lazarus installation (examples/gridexamples/gridcelleditor).