views:

405

answers:

4

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:

  1. The time value for the BeginDate column must always be 00:00:00
  2. The time value for the EndDate column 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.

A: 

I'm not sure I follow, the user has to enter a value for the two dates but they are always the same? If I understand correctly, why not set default values in the database , then when you read into the dataset the values will already be there

Stuart
I think the OP means that the time portion of the DateTime must be the same, but that the date can be anything.
ChrisF
Sorry if I caused any confusion - I've edited my post to make things clearer.
Thorsten Dittmar
+1  A: 

You can add the following lines to your CellValidating method, after your other validations

DateTime newValue = oldValue.Date;

and

DateTime newValue = oldValue.Date.AddDays(1).AddSeconds(-1);
Noam Gal
+2  A: 

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.

ChrisF
A: 

You can also do the validation inside your property setter:

 public DateTime BeginTimeStamp
 {
     get { return _dateTime; }
     set
     {
         // force the time to whatever you want
         _dateTime = value.Date;
     }
 }
Groo