DateTimePicker 显示今天的日期而不是显示其实际值

发布于 2024-08-08 23:03:44 字数 517 浏览 10 评论 0原文

我们在表单上的自定义用户控件上有几个 DateTimePicker。它们是可见的,但未启用(仅用于显示目的)。加载 UserControl 时,DateTimePicker 会从来自 DataSet 的 DataRow 分配值,该 DataSet 存储从 SQL Server 存储过程返回的单个记录。

存在不一致的行为,用户有时会看到今天的日期,而不是分配给 DateTimePicker 的日期。将所需的日期分配给 .Value 属性还是 .Text 属性似乎并不重要:

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

我希望在上面的两个语句中,使用 Value 属性的语句更合适。但是,在这两种情况下,无论数据库中的值如何,都会向用户显示今天的日期。对于 txtstart.Value,Visual Studio 向我显示该值已按预期分配。那么为什么它不向用户显示该日期而不是今天的日期呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-08-15 23:03:44

我找到了答案。您必须将复选框值设置为选中以指示非空值。

this.dateSold.Checked = true;
// 根据需要设置为 true 或 false
this.dateSold.ShowCheckBox = false;

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;

小ぇ时光︴ 2024-08-15 23:03:44

我也遇到了这个问题,发现您确实需要将 .Checked 值设置为 True

我在属性窗口中设置了 Checked 属性,但它仍然不起作用,所以我只是在分配值之前在代码中设置它,这解决了问题。

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.

仙女山的月亮 2024-08-15 23:03:44

拥有多个 DateTimePicker 似乎是一个问题。我能够通过以编程方式使用我想要的值创建 DateTimePickers 并将它们添加到表单中来解决这个问题(错误?)。

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.

挽手叙旧 2024-08-15 23:03:44

我最终接受了 AB 诺兰的建议。我一直不清楚禁用 DateTimePicker 背后的原因,所以回到 2009 年 10 月 23 日,我没有继续对控件大惊小怪,而是在 TextBoxes 中显示了我想要的日期。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文