1

i have a datagridView with a cell whose data type is Datetime. The value of the cell is formatted as HH:mm (hours, minutes). When the user edits the cell with a value like 5, I would like to transform it into 05:00 before the datagridView complains that the value entered is not a valid datetime.

I have tried the validating event but there I cannot change the Formatted value, I can only Cancel. I know what I am asking is possible because I have done it in another project, but I cannot access the code of that project to see how I did it. Please help. Thanks

user1238784
  • 2,250
  • 3
  • 22
  • 41
  • You can change the Value, can't you`? The FormattedValue is created by the system. – TaW Oct 18 '18 at 10:15
  • Hi, in which event do I change the value? – user1238784 Oct 18 '18 at 10:17
  • Right in `CellValidating` with appropriate checks, of course. – TaW Oct 18 '18 at 10:18
  • But if even if I change the value in cellValidating, the datagridview will still complain that the value entered by the user is wrong – user1238784 Oct 18 '18 at 10:21
  • Hm, did you set `e.Cancel = false;` ? Are there other validations? – TaW Oct 18 '18 at 10:24
  • At the moment I have nothing in the cellvalidating event. I can only set e.Cancel = true there, If I do not, the Grid will popup an error that the data entered is not a valid datetime. – user1238784 Oct 18 '18 at 10:36
  • Well if the column needs `DateTime` then you must set the `Value` to a valid `DateTime`, of course.Neither `5` nor `05:00`. – TaW Oct 18 '18 at 10:46
  • The system recognizes 5:00 when it is entered because that is the format of time. So it uses the Datetime value and changes only the part of the time – user1238784 Oct 18 '18 at 11:07

1 Answers1

1

I have solved the problem using the CellParsing event. There I get the Value entered from the user and I can parse it into a proper datetime and finally set e.ParsingApplied = true (very important otherwise the parsing you did is ignored)

user1238784
  • 2,250
  • 3
  • 22
  • 41