Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 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.

VB
'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? 
Posted
Comments
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?
—SA
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.
Sergey Alexandrovich Kryukov 12-Mar-14 0:20am    
I think we already discussed that: the root of the problem was you migration from Oracle; this is explainable.
—SA

1 solution

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:
http://www.sqlite.org/datatype3.html[^],
http://www.sqlite.org/lang_datefunc.html[^].

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 http://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx[^].

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

—SA
 
Share this answer
 
Comments
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.
Sergey Alexandrovich Kryukov 12-Mar-14 0:18am    
I agree, this is the problem...
—SA
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.
Sergey Alexandrovich Kryukov 12-Mar-14 0:19am    
This is strange... the value should consistency update the view, this is what property setters are designed for...
—SA
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900