views:

61

answers:

1

I have a DateTimePicker cell in my DataGridView. I'd like to be able to enter edit mode and drop the calendar when a button is clicked. I'm able to do the first part without difficulty but the second isn't working. If I have a standalone DateTimePicker the SendKeys call does work as expected.

//Select the cell and enter edit mode -  works
myDGV.CurrentCell = myDGV[calColumn.Index, e.RowIndex];
myDGV.BeginEdit(true);

//Send an ALt-Down keystroke to drop the calendar  - doesn't work
SendKeys.SendWait("%{DOWN}");

From debugging I believe that the problem is that the keystroke is being sent to the DGV and not the specific cell that I'm trying to edit. The reason I think is is that I've put code to log keys received by the grids KeyPress and KeyDown events. They log my arrowing around the grid and the keys sent by SendKeys, but not those from when I'm editing a cell by typing in it.

+1  A: 

Please see my answer on C# Winforms DataGridView Time Column. I believe it will fit your needs perfectly. You can also use it for a column that has a ComboBox.

0A0D
It does, thanks.
Dan Neely
@0A0D On second look your solution is missing the final step. Sending the selection from the DTP back to the grid/datasource.
Dan Neely
@Dan: That's done in a separate step not shown.
0A0D
@0A0D OK. I was just curious how you went about it, because my implementation required changes to one of the methods you provided. I saved the cell location and set a flag in the setNewCellDate method, and then checked for the flag and used the cell location to save the data in the DateTimePicker's VisibleChanged event.
Dan Neely
@Dan: Whatever works for you but this method is certainly better than messing with a DateTimePicker cell :)
0A0D
I ended up moving the code to save from the VisibleChanged event to the Leave and KeyPress (if the user pressed enter) events to handle tabing out of the DTP and closing it by pressing enter respectively. This also required setting tab order so that the DTP is immediately followed by the DGV.
Dan Neely