DateTimePicker displays today's date instead of displaying its actual Value

被刻印的时光 ゝ 提交于 2020-01-03 08:07:11

问题


We have a couple of DateTimePickers on a custom UserControl, on a Form. They are visible, but not enabled (for display purposes only). When the UserControl is loading, the DateTimePickers are assigned values from a DataRow that came from a DataSet which stores a single record returned from a SQL Server stored procedure.

There is an inconsistent behavior in which the users sometimes see today's date instead of the date that was assigned to the DateTimePicker. It doesn't seem to matter whether I assign the date I want to the .Value property or the .Text property:

txtstart.Value = (DateTime) dr["Group_Start_Date"];
txtend.Text = dr["Term_Date"].ToString();

I expect that of the two statements above, the one using the Value property is more appropriate. But, in both cases, today's date is displayed to the user regardless of the values that were in the database. In the case of txtstart.Value, Visual Studio shows me that the value was assigned as expected. So why isn't it displaying that date to the user instead of today's date?


回答1:


I found the answer. You MUST set the checkbox value to checked to indicate a non-null value.

this.dateSold.Checked = true; // set to true or false, as desired this.dateSold.ShowCheckBox = false;




回答2:


It seems to be a problem with having multiple DateTimePickers. I was able to get around the issue (bug?) by programatically creating the DateTimePickers with the values I wanted and adding them to the form.




回答3:


I ended up going with AB Nolan's suggestion. The reasoning behind a disabled DateTimePicker was never clear to me, so back on 10/23/2009, rather than continue fussing with the control I just displayed the dates I wanted in TextBoxes instead.




回答4:


I was having trouble with this too and found that you indeed need to set the .Checked value to True.

I set the Checked property in the properties window and it still didn't work so I just set it in code before assigning the value and that fixed the problem.



来源:https://stackoverflow.com/questions/1615294/datetimepicker-displays-todays-date-instead-of-displaying-its-actual-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!