2

I have some funny (?!?) issues with the DateTimePicker and hopefully someone can help me out. I have a form with a DateTimePicker on it and I want to bind it to a property of a custom class. DateTimePicker has a custom format set to dd.MM.yyyy HH:mm:ss. Here is what I have tried and the troubles with those tries:

  • I made a binding to the Value property of DateTimePicker. The property of my custom class contains a valid date. When I run the app I get an ArgumentOutOfRangeException stating that "01.01.0001 00:00:00" is not a valid value for value and it should be between MinDate and MaxDate. (But I can't set neither DateTime.MaxValue nor DateTime.MinValue to the value property!)
  • I made a binding to the Text property of DateTimePicker. All is running well, but the seconds are always shown as "00". I can enter different values and they are reflected to the bound property of my custom class!

Any ideas?

Edit Here is the code snippet out of the designer file:

this.dateTimePickerTimestampFrom.CustomFormat = "dd.MM.yyyy HH:mm:ss";
this.dateTimePickerTimestampFrom.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSourceSelectLogEntries, "DateFrom", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.dateTimePickerTimestampFrom.Enabled = false;
this.dateTimePickerTimestampFrom.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.dateTimePickerTimestampFrom.Location = new System.Drawing.Point(81, 42);
this.dateTimePickerTimestampFrom.Name = "dateTimePickerTimestampFrom";
this.dateTimePickerTimestampFrom.Size = new System.Drawing.Size(147, 20);
this.dateTimePickerTimestampFrom.TabIndex = 3;

Edit 2 The bindingsource is a custom class containing a couple of properties. The values are valid at the moment the binding is set. I set it in the following code:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    // Exception is thrown at the following line.
    // controller is an instance of my custom class containing valid values.
    bindingSourceSelectLogEntries.DataSource = controller; 
}
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • 1
    Related (for reference): http://stackoverflow.com/questions/8359380/datetimepicker-and-seconds. Can you paste some code? Maybe you did a "dumb" mistake that you didn't see. – Otiel Dec 07 '11 at 08:03
  • @Otiel: I did all the bindings and settings in the property sheet of the designer. I'll post the relevant lines. – Fischermaen Dec 07 '11 at 08:07
  • Wich .net and visual studio version you are using ? – dknaack Dec 07 '11 at 08:16
  • @dknaack: I use VisualStudio 2010 SP1 and the target framework is ".NET Framework 4". (**Not** the client profile) – Fischermaen Dec 07 '11 at 08:18
  • The minimum date and time that can be selected in the control is '1/1/1753 00:00:00'. You can try to set '01.01.0001 00:00:00' but control by default changes minimum date to '1/1/1753 00:00:00' – Renatas M. Dec 07 '11 at 08:20
  • 1
    @Fischermaen i tried your code and it works fine for me. Thats strange. – dknaack Dec 07 '11 at 08:21
  • @Reniuz and dknaack: indeed, that is strange... – Fischermaen Dec 07 '11 at 08:25
  • Just to clarify: what is your binding source? are you sure that it has DateTimes and seconds? – sq33G Dec 07 '11 at 08:28
  • @sq33G: See "Edit 2" of my question, I've added the information you've requested. – Fischermaen Dec 07 '11 at 08:32
  • IMHO controls mindate is set to strange 1753 because of sqls mindate. Read [here](http://stackoverflow.com/a/806110/754438) for more explanation about mindates. – Renatas M. Dec 07 '11 at 08:32
  • The edit doesn't really help me, it just changes my question to "are you sure that `controller` has DateTimes with seconds?" – sq33G Dec 07 '11 at 08:51
  • @sq33G: Oh sorry, I thought I was clear enough, when I said "it contains valid values". Just to clearify, the controller contains a valid date with a time portion containing seconds differs from "00". – Fischermaen Dec 07 '11 at 08:55
  • One more hint to all specialists: The exception raises *before* the bound property of the custom class ´controller´ is requested! – Fischermaen Dec 07 '11 at 09:53

2 Answers2

1

Just an FYI NULL is not an issue here as much as what the value representing NULL is.

There is a mismatch with the defaults within the framework that many (including myself) miss most of the time.

Until recently and in some cases even now, in MSSQL and most other DB the default representation for NULL values in DateTime fields was date value of "1900-01-01". Now it has been changed to "01/01/0001" Later is also the value of DateTime.MinValue in MS visual studio framework.

However, the datePicker control does not recognize this value as valid date because standard DatePicker control in VS has minDate = '01/01/1758'. and there is the issue that triggers the error condition above.

You are getting a perfectly valid NULL field representation from your DB which is outside of the acceptable range of the DatePicker control.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
vlad
  • 106
  • 6
1

I found out the solution by myself and I would like to tell, so others may not spend so much time on the same problem like I did.

My design consists of a form and a controller (containing the data to present). In the form I wanted to do everything in the designer to minimize the code behind. So I had a binding source on the form, having an instance of my controller as DataSource. ReflectorPro helped me to find out, that the CurrencyManager was involved because the BindingSource was treated as a list. Therefore all values of all binded controls were set to null prior to get the values from the controller. DateTimePicker doesn't like null as a value for its Value property and throws the exception.

Now I did the binding manually in code behind and everything works as I expected.

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • Glad you got it sorted out ;) I also like to use the designer as often as possible but it can be tricky sometimes. – Otiel Dec 07 '11 at 22:04