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

8
  • You can change the Value, can't you`? The FormattedValue is created by the system.
    – TaW
    Commented Oct 18, 2018 at 10:15
  • Hi, in which event do I change the value? Commented Oct 18, 2018 at 10:17
  • Right in CellValidating with appropriate checks, of course.
    – TaW
    Commented Oct 18, 2018 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 Commented Oct 18, 2018 at 10:21
  • Hm, did you set e.Cancel = false; ? Are there other validations?
    – TaW
    Commented Oct 18, 2018 at 10:24

1 Answer 1

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.