简体   繁体   English

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

[英]C#: How to adjust the value the user entered in DataGridView?

I have a databound DataGridView. 我有一个数据绑定的DataGridView。 The data source is a typed data set with a table containing two DateTime columns ( BeginTimeStamp and EndTimeStamp ). 数据源是带有表的类型化数据集,该表包含两个DateTime列( BeginTimeStampEndTimeStamp )。 I read and write the data to an SQL Server 2005 database using the typed data set's Update command. 我使用键入的数据集的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. 用户必须在两列中的每一列中输入一个日期,我使用CellValidatingRowValidating事件来强制执行此CellValidating 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 BeginDate列的时间值必须始终为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) 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 . 用户输入2009/01/01 01:00:00 pm作为BeginTimeStamp My application should change this to 2009/01/01 00:00:00. 我的应用程序应将此更改为2009/01/01 00:00:00。

The user enters 2009/01/31 01:00:00pm as EndTimeStamp . 用户输入2009/01/31 EndTimeStamp pm作为EndTimeStamp My application should change this to 2009/01/31 23:59:59. 我的应用程序应将此更改为2009/01/31 23:59:59。

I'd just display the DateTime as a Date and add the time behind the scenes. 我只是将DateTime显示为Date并在幕后添加时间。

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的答案。

You can add the following lines to your CellValidating method, after your other validations 在进行其他验证之后,可以CellValidating添加到CellValidating方法中

DateTime newValue = oldValue.Date;

and

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

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 如果我理解正确,为什么不在数据库中设置默认值,那么当您读入数据集时,这些值将已经存在

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM