I have a databound DataGridView. The data source is a typed data set with a table containing two DateTime columns (BeginTimeStamp and EndTimeStamp). I read and write the data to an SQL Server 2005 database using the typed data set’s Update command.
The user must enter a date into each of the two columns, which I enforce using the CellValidating and RowValidating events. However, I also need to make sure that the following two rules apply:
- The time value for the
BeginDatecolumn must always be 00:00:00 - The time value for the
EndDatecolumn must always be 23:59:59 (or 11:59:59 pm if you like)
As I do not want the user to enter the 23:59:59 all the time, I’d like to somehow change the user’s inputs according to 1. and 2. in my code.
Where and how would I do that?
EDIT
Sorry in case I was unclear. The user may enter any date part, however, the time part is fixed at midnight for the BeginTimeStamp and 23:59:59 for the EndTimeStamp.
Example:
The user enters 2009/01/01 01:00:00pm as BeginTimeStamp. My application should change this to 2009/01/01 00:00:00.
The user enters 2009/01/31 01:00:00pm as EndTimeStamp. My application should change this to 2009/01/31 23:59:59.
I’d just display the DateTime as a Date and add the time behind the scenes.
This could be when the user enters the data or equally it could be when you write the data to the database.
If you choose the former then look at the DataGridView.CellEndEdit event.
See Noam’s answer for the code to set the time appropriately.