更改DateTimePicker文本框值 [英] Change the DateTimePicker textbox value
问题描述
好的,所以DateTimePicker控件让我疯了。我正在尝试更改控件值并在加载表单时显示代码。我基本上是从sqlite表中提取最小和最大日期,然后我想将它传递给两个DateTimePicker控件。这些控件允许用户根据他们选择的日期范围更新分析选项卡上的数据网格/图表,但我希望它以最小和最大日期开始,以便首先显示所有数据。我的DateTimePicker控件使用自定义格式(yyyy-MM-dd),这可能会导致一些问题。似乎我实际上正确地传递了值,并且它使用了值(即'2014-02-23'到'2014-03-12'),但是文本框显示了今天的日期。当我尝试在DateTimePicker控件中更改值,并希望文本时,我在下面的代码中做错了什么?提前致谢。
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?
推荐答案
这是因为你在第一时间做错了。为什么你甚至需要字符串date1
和date2
?您不应该尝试使用表示数据的字符串(并且在日期/时间的情况下,甚至是模糊的),而是使用数据本身。如果您将时间数据存储在SQLite中,那么SQLite的设计方式就是使用它:
http:// www.sqlite.org/datatype3.html [ ^ ],
http://www.sqlite.org/lang_datefunc.html [ ^ ]。
在.NET站点上,不接触字符串表示形式,将时间数据转换为System.DateTime
。由于您只使用日期,您可以使用 System.DateTime /system.datetime.adddays(v=vs.110).aspx\">http://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx [ ^ ]。
如果您仍想使用字符串,请保留第一行,您可以在其中为<$ c $的实例指定值c> DateTimePicker 。
-SA
This is because you did it wrong in first place. Why would you even need stringsdate1
anddate2
? 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 intoSystem.DateTime
. As you are using only dates, you can convert integer number toSystem.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 yourDateTimePicker
.
—SA
这篇关于更改DateTimePicker文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!