C# 如何将 DateTimePicker 控件设置为特定日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/541225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:25:33  来源:igfitidea点击:

How can I set a DateTimePicker control to a specific date?

c#winformsdatepicker

提问by

How can I set a DateTimePicker control to a specific date (yesterday's date) in C# .NET 2.0?

如何在 C# .NET 2.0 中将 DateTimePicker 控件设置为特定日期(昨天的日期)?

采纳答案by Rowland Shaw

Just need to set the value property in a convenient place (such as InitializeComponent()):

只需要在方便的地方设置 value 属性(比如InitializeComponent()):

    dateTimePicker1.Value = DateTime.Today.AddDays(-1);

回答by Jason Punyon

This oughta do it.

这个应该做

DateTimePicker1.Value = DateTime.Now.AddDays(-1).Date;

回答by Sessiz Saat

You can set the "value" property

您可以设置“值”属性

dateTimePicker1.Value = DateTime.Today;

回答by lc.

Use the Valueproperty.

使用Value物业。

MyDateTimePicker.Value = DateTime.Today.AddDays(-1);

DateTime.Todayholds today's date, from which you can subtract 1 day (add -1 days) to become yesterday.

DateTime.Today保存今天的日期,您可以从中减去 1 天(加 -1 天)成为昨天。

DateTime.Now, on the other hand, contains time information as well. DateTime.Now.AddDays(-1)will return this time one day ago.

DateTime.Now,另一方面,也包含时间信息。DateTime.Now.AddDays(-1)将在一天前的这个时候返回。

回答by lc.

dateTimePicker1.Value = DateTime.Today();

回答by Lê Quang Duy

If you want to set a date, DateTimePicker.Value is a DateTime object.

如果要设置日期,DateTimePicker.Value 是一个 DateTime 对象。

DateTimePicker.Value = new DateTime(2012,05,28);

This is the constructor of DateTime:

这是 DateTime 的构造函数:

new DateTime(int year,int month,int date);

My Visual is 2012

我的视觉是 2012

回答by Kakha Middle Or

Can't figure out why, but in some circumstances if you have bound DataTimePicker and BindingSource contol is postioned to a new record, setting to Value property doesn't affect to bound field, so when you try to commit changes via EndEdit() method of BindingSource, you receive Null value doesn't allowed error. I managed this problem setting direct DataRow field.

不知道为什么,但在某些情况下,如果您已绑定 DataTimePicker 并且 BindingSource 控件被定位到新记录,则设置为 Value 属性不会影响绑定字段,因此当您尝试通过 EndEdit() 方法提交更改时的 BindingSource,您收到 Null 值不允许错误。我管理这个问题设置直接 DataRow 字段。

回答by Nirav Raval

Also, we can assign the Value to the Control in Designer Class (i.e. FormName.Designer.cs).

此外,我们可以将值分配给设计器类(即 FormName.Designer.cs)中的控件。

DateTimePicker1.Value = DateTime.Now;

This way you always get Current Date...

这样你总能得到当前日期......

回答by Traderhut Games

FYI: If you are setting the value, and not seeing anything - you might check to see if you have a 'CustomFormat' set - I just hit this and it was set to ' ' for the 1/1/1900 value (our 'not set' value) and set to MM/dd/yyyy if not.

仅供参考:如果您正在设置该值,但没有看到任何内容 - 您可能会检查是否设置了“CustomFormat” - 我刚刚点击了这个,它被设置为 ' ' 表示 1/1/1900 值(我们的 '未设置'值),如果不是,则设置为 MM/dd/yyyy。