Click here to Skip to main content
14,176,972 members
Rate this:
Please Sign up or sign in to vote.
See more:
Okay, so the DateTimePicker control has been driving me nuts. I'm trying to change the control value and display through code when I load my form. I'm essentially pulling the min and max date from a sqlite table and I then want to pass it to two DateTimePicker controls. These controls allow the users to update datagrids/graphs on an analytics tab based on the date range they select, but I want it to start out with the min and max dates so it shows all of the data at first. My DateTimePicker control is using a custom format (yyyy-MM-dd), which may be causing some problems. It seems that I'm actually passing the values correctly, and it's using the values (i.e. '2014-02-23' to '2014-03-12'), but the textbox shows today's date. What am I doing wrong in the code below when I try to change the value, and hopefully the text, in the DateTimePicker control? Thanks in advance.

'DateTimePicker controls have a format of yyyy-MM-dd
'date1='2014-02-23 20:38:30'
DateTimePicker1.Value = DateTime.Parse(date1).ToString("yyyy-MM-dd") 
DateTimePicker1.Text = DateTimePicker1.Value.ToString("yyyy-MM-dd")
'Does .Text do anything? 

'date2='2014-03-12 13:02:58'
DateTimePicker2.Value = DateTime.Parse(date2).ToString("yyyy-MM-dd") 
DateTimePicker2.Text = DateTimePicker2.Value.ToString("yyyy-MM-dd")
'Does .Text do anything? 
Sergey Alexandrovich Kryukov 11-Mar-14 22:00pm
Why would you do all that?
Why would you need to get the string values data1 and date2 in first place?
Phutile 11-Mar-14 22:08pm
How else would I get a min and max value in the DateTimePicker when the app loads? Having the value in there also helps the user to see where the dataset starts and ends for audit dates.
I think we already discussed that: the root of the problem was you migration from Oracle; this is explainable.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is because you did it wrong in first place. Why would you even need strings date1 and date2? You should not to try to work with strings representing data (and, in case of date/time, even ambiguous), but work with data itself. If you store time data in SQLite, the way SQLite is designed to work with it:[^],[^].

On .NET site, not touching string representation, convert time data into System.DateTime. As you are using only dates, you can convert integer number to System.DateTime using[^].

If you still want to work with strings, leave just first line, where you assign value to the instance of your DateTimePicker.

Phutile 11-Mar-14 22:32pm
Thanks Sergey. I guess some of my trouble comes from mostly working with Oracle and having been used to the date datatype. Unfortunately SQLite doesn't have this, so I opted for the string method... probably not the best, but it was easiest for me to see what was going on. I also wanted my datetimepicker control to remove the time part and just use the dates. I will look at this further and see if I can modify my code. Thanks.
I agree, this is the problem...
Phutile 11-Mar-14 22:38pm
Qquick question, even if I work with strings, why doesn't the first line I use actually change the value visible on the DateTimePicker? It's changing the value behind the scenes, but not the text displayed in the DateTimePicker control.
This is strange... the value should consistency update the view, this is what property setters are designed for...
Phutile 12-Mar-14 0:22am
I agree it's strange. That's why it's been driving me nuts. I have looked at this for several days trying different way to feed the datetimepicker control, yet nothing would change the control's text... only the value behind it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190526.1 | Last Updated 11 Mar 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100