在 C# 中的 DateTimePicker 中设置空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14723316/
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
Set Null value in DateTimePicker in C#
提问by Kiran
I am developing a winform
based Desktop application in C#. I would like the user to set the DateTimePicker to Null
. I am developing a search box, and would like to ignore the date if it is set to Null
我正在winform
用 C#开发一个基于桌面的应用程序。我希望用户将 DateTimePicker 设置为Null
. 我正在开发一个搜索框,如果设置为日期,我想忽略它Null
Here is what I am doing :
这是我在做什么:
this.dateTimePicker2.CustomFormat = " ";
this.dateTimePicker2.Format = DateTimePickerFormat.Custom;
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
this.dateTimePicker2.CustomFormat = "dd-MM-yy";
}
So far so good. However, once the user selects the date, the dateTimePicker2
control shows some date ( the date the user has selected). There is no way to set the date to null again. I am not keen to enable the checkbox associated with the datetimepicker
control.
到现在为止还挺好。但是,一旦用户选择了日期,dateTimePicker2
控件就会显示某个日期(用户选择的日期)。无法再次将日期设置为空。我不想启用与datetimepicker
控件关联的复选框。
I was wondering if it is possible to set the datetimepicker
date to null
.
我想知道是否可以将datetimepicker
日期设置为null
.
Thanks
谢谢
回答by sa_ddam213
I dont think its possible because DateTime is not nullable.
我认为这是不可能的,因为 DateTime 不可为空。
But you could use DateTime.MinValue
this way tou can still compare easily
但是你可以用DateTime.MinValue
这种方式你仍然可以很容易地比较
if (datetimepicker.DateTime == DateTime.MinValue)
{
// just as good as null, maybe
}
回答by MethodMan
This is in reference from a post that is old but other users on this site have posted it here you go
这是参考旧帖子,但此站点上的其他用户已将其发布到此处
// Use ValueChanged to decide if the value should be displayed:
dateTimePicker1.ValueChanged += (s, e) => { dateTimePicker1.CustomFormat = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? "MM/dd/yyyy" : " "; };
//When getting the value back out, use something like the following:
DateTime? dt = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? (DateTime?) dateTimePicker1.Value : null;
// or
DateTime dt2 = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? dateTimePicker1.Value : DateTime.MinValue;
or you can set the CustomFormat to " " an empty space like the following below
或者您可以将 CustomFormat 设置为 " " 一个空白区域,如下所示
dateTimePicker1.CustomFormat= " ";
回答by Dennis
I was wondering if it is possible to set the datetimepicker date to null
我想知道是否可以将 datetimepicker 日期设置为 null
There's no way to set DateTimePicker.Value
to null
, because its type isn't nullable. MSDN says, that:
无法设置DateTimePicker.Value
为null
,因为它的类型不可为空。MSDN 说:
If the Value property has not been changed in code or by the user, it is set to the current date and time (DateTime.Now).
如果代码中或用户未更改 Value 属性,则将其设置为当前日期和时间 (DateTime.Now)。
But this isn't a problem. You should set to null some bound property, not the DateTimePicker.Value
:
但这不是问题。您应该将某些绑定属性设置为 null,而不是DateTimePicker.Value
:
public class MyModel : INotifyPropertyChanged
{
public DateTime? DateTimeFilter
{
get { return dateTimeFilter; }
set
{
if (dateTimeFilter != value)
{
dateTimeFilter = value;
OnPropertyChanged("DateTimeFilter");
}
}
}
private DateTime? dateTimeFilter;
// INotifyPropertyChanged implementation is omitted
}
public partial class Form1 : Form
{
private readonly MyModel model;
public Form1()
{
InitializeComponent();
model = new MyModel();
dateTimePicker1.DataBindings.Add("Value", model, "DateTimeFilter", true, DataSourceUpdateMode.OnPropertyChanged, DateTime.Now);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(model.DateTimeFilter.HasValue ? string.Format("User has selected '{0}'.", model.DateTimeFilter) : "User hasn't selected anything.");
}
private void button2_Click(object sender, EventArgs e)
{
// here's the data binding magic: our model's property becomes null,
// and datetimepicker's value becomes DateTime.Now, as it was initially set
model.DateTimeFilter = null;
}
}
回答by Praditha
DateTime is not nullable type.
But you can do this trick:
Just add a checkbox on the date picker, you can find it on properties and set ShowCheckBoxto True.
You can use this conditional:
DateTime 不是可为空类型。
但是你可以这样做:
只需在日期选择器上添加一个复选框,你可以在属性中找到它并将ShowCheckBox设置为True。
您可以使用此条件:
if(datePicker.Checked){
//Do some stuff to ignore the Date Picker value
}
For additional info, I'm use Microsoft Visual Studio 2010, I'm not check the other IDE yet.
Hope this help.
Cheers...
有关其他信息,我使用的是 Microsoft Visual Studio 2010,我还没有检查其他 IDE。
希望这有帮助。
干杯...
回答by Dave Stuart
This worked for me when using a DateTimeControl on a SharePoint Application Page.
在 SharePoint 应用程序页面上使用 DateTimeControl 时,这对我有用。
if (dtcOpenDate.SelectedDate != DateTime.Now)
{
item["Open Date"] = dtcOpenDate.SelectedDate;
}
回答by Sagin Joy
Add checkbox
to your datetime
picker property:
添加checkbox
到您的datetime
选择器属性:
string datevalue = dateTimePicker2.Checked != true ? "N/A": dateTimePicker2.Text;**