C#:如何调整用户在DataGridView中输入的值?

时间:2022-09-23 19:58:14

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.

我有一个数据绑定DataGridView。数据源是一个类型化的数据集,其中包含两个DateTime列(BeginTimeStamp和EndTimeStamp)。我使用类型化数据集的Update命令读取和写入数据到SQL Server 2005数据库。

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:

用户必须在两列中的每一列中输入日期,我使用CellValidating和RowValidating事件强制执行。但是,我还需要确保以下两条规则适用:

  1. The time value for the BeginDate column must always be 00:00:00
  2. BeginDate列的时间值必须始终为00:00:00

  3. The time value for the EndDate column must always be 23:59:59 (or 11:59:59 pm if you like)
  4. EndDate列的时间值必须始终为23:59:59(如果您愿意,则为11:59:59 pm)

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.

由于我不希望用户一直进入23:59:59,我想以某种方式根据我的代码中的1.和2.更改用户的输入。

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.

对不起,我不清楚。用户可以输入任何日期部分,但是,对于BeginTimeStamp,时间部分固定为午夜,对于EndTimeStamp,时间部分固定为23:59:59。

Example:

The user enters 2009/01/01 01:00:00pm as BeginTimeStamp. My application should change this to 2009/01/01 00:00:00.

用户输入2009/01/01 01:00:00 pm作为BeginTimeStamp。我的申请应该改为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.

用户输入2009/01/31 01:00:00 pm作为EndTimeStamp。我的申请应该改为2009/01/31 23:59:59。

4 个解决方案

#1


I'd just display the DateTime as a Date and add the time behind the scenes.

我只是将DateTime显示为日期并添加幕后时间。

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.

如果选择前者,请查看DataGridView.CellEndEdit事件。

See Noam's answer for the code to set the time appropriately.

请参阅Noam的代码答案,以便适当地设置时间。

#2


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

在进行其他验证后,可以将以下行添加到CellValidating方法中

DateTime newValue = oldValue.Date;

and

DateTime newValue = oldValue.Date.AddDays(1).AddSeconds(-1);

#3


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

我不确定我是否关注,用户必须输入两个日期的值,但它们总是相同的?如果我理解正确,为什么不在数据库中设置默认值,那么当您读入数据集时,值已经存在

#4


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;
     }
 }

#1


I'd just display the DateTime as a Date and add the time behind the scenes.

我只是将DateTime显示为日期并添加幕后时间。

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.

如果选择前者,请查看DataGridView.CellEndEdit事件。

See Noam's answer for the code to set the time appropriately.

请参阅Noam的代码答案,以便适当地设置时间。

#2


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

在进行其他验证后,可以将以下行添加到CellValidating方法中

DateTime newValue = oldValue.Date;

and

DateTime newValue = oldValue.Date.AddDays(1).AddSeconds(-1);

#3


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

我不确定我是否关注,用户必须输入两个日期的值,但它们总是相同的?如果我理解正确,为什么不在数据库中设置默认值,那么当您读入数据集时,值已经存在

#4


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;
     }
 }