DateTimePicker to show empty content

Answered
0
0

Hi.

I have a legacy database where all DateTime columns have the date 01/01/1800 meaning “Null” dates. So, no null is allowed and when we want to mean a datetime is empty, 01/01/1800 is saved on its place.
Then we had DateTime controls that would show empty when the content was 01/01/1800 and set 01/01/1800 when the user cleared the control content.

In our Wisej DateTimePicker, I thought that if I set the MinDate to this 01/01/1800 date I would be done with it, but it doesn’t seem to work this way. Moreover, if I clear the DateTimePicker content at run-time, which is bound to a DateTime property, I get the control have a red border not accepting it to be empty.

Would there be a trick to be able to first, be able to empty the DateTimePicker content, and second, make it set its Value to 01/01/1800 once cleared?

Thanks in advance.

Ivan
(VS 2019, C#, Wisej 2.2.38.0)

  • You must to post comments
Best Answer
0
0

Ivan,

You can use this in the DropDown event that Alaa mentioned.

this.dateTimePicker1.Eval($”this.getChildControl(‘list’).setValue(new Date(‘{new DateTime(2021,1,15).ToString(“yyyy-M-d”)}’))”);

Or you can use the NullableValue property and set it to null or set Text to “”. The DateTime.MinValue  is 1/1/0001 so 1800 is a valid date.

HTH

 

  • You must to post comments
Good Answer
0
0

Hi Ivan,

When you said that you want to “clear” the DateTimePicker, do you mean that you want to set the value of the control to be empty or just show the desired date?
If that’s what you’re looking for I have attached a file to what could be a resolution to your problem.

If that’s not the case, can you please provide me with some more information?
Maybe you’re using some UserControls?

Best regards,
Alaa

  • You must to post comments
0
0

Thank you Luca!
It worked like a charm.
I was trying to set the control.Value at the DropDown. Using your JS was exactly what I needed.

I tried using the NullableValue to start with, but got many exceptions thrown while doing that. Maybe I didn’t do it the proper way. I will check further.

  • Luca
    • Luca
    • Feb 17, 2021 - 4:31 pm
    We’ll probably fix the DateTimePicker to update the dropped down calendar when already opened. It looks like it was already supposed to do that.
  • Ivan Borges
    Makes sense, but I still prefer your solution in my case, since I am now able to navigate the Calendar to Todays date without setting any value to the Control itself, wait for the user to choose (or not) any given date.
  • You must to post comments
0
0

Hi again, Alaa.

I was able to accomplish showing a blank box on a pre-set datetime using the Custom Format.

Now I have a side-effect, which happens once the user goes to the control to Enter some date. All fine if they type it, but if they choose to use the Calendar tool on the box, it will show a date way behind, in 01/01/1800. 😀
OK, this might be one of those “you can’t have the cake and eat it too”, but would you know of a way to set this Calendar to Todays date programmatically when the user enters it, if necessary?

  • Alaa (ITG)
    Hi Ivan, You can use the GotFocus event to programmatically set the Calendar to today’s date. For example: private void Window1_Load(object sender, EventArgs e) { dateTimePicker1.Value = DateTime.Parse(“01-01-1800”); //For testing perpouses dateTimePicker1.GotFocus += new System.EventHandler(this.ChangeDate); } private void ChangeDate(object sender, EventArgs e) { dateTimePicker1.Format = DateTimePickerFormat.Short; dateTimePicker1.Value = DateTime.Now; } Please let me know if this fits your needs. Best regards, Alaa
  • Ivan Borges
    Yep, that would be one way, except if the user is just tabbing through the controls and then once it goes through the DateTimePicker it would set its values even if this is not what the user meant to do. If I had an event to the Calendar click or something like it, it would work. I have the feeling I will have to work with the DateTimePicker CheckBox and make the user click it in order to edit it when it has an empty value. Thanks Alaa.
  • Alaa (ITG)
    You can also use the DropDown event so that the date would change only when the user clicks on the calendar.
  • Ivan Borges
    Yep, I did that. But it seems to be a bit too late at the DropDown event. When you click on it, it will show the date set before, even if you set a new date on it. Then, if you close the Calendar and click on it again, it works… :-(
  • You must to post comments
0
0

Hi Alaa.

Thanks so much for the reply.

Sorry, with “clear” I meant the control to be empty. So, if the user empties its content, what would be a NULL date, I want its bound property to be set to 01/01/1800. On the other hand, if the property value returns 01/01/1800, I would like the control to show an empty space, and of course, let the user type or choose any date they want.

And no, so far I am not using any UserControl for this.

I can be confusing at times (most of them), so ask me anything you want to make it clear. 🙂

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.