设置的DateTimePicker值为空 [英] Set DateTimePicker value to be null

查看:244
本文介绍了设置的DateTimePicker值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发这个UI。我有2个的DateTimePicker控制。起初我想设置控件的值设置为空,直到用户选择一个日期,因此,如果用户不选择的日期,它会通过为空到数据库中的值。默认情况下它需要当前日期。
可以PLZ提出好的建议。或附加一段代码,这将帮助我。
我已经尝试设置minDate属性,这是行不通的。
的代码是用C#.net。

I am developing this UI. I have 2 DateTimePicker controls. Initially i want to set the value of the controls to be null, until a user selects a date and hence if user do not select the date, it will pass the value as null to the data base. By default it takes current date. Can you plz suggest something. or attach a piece of code which will help me out. I have tried setting the MinDate property, it doesn't work. The code is in C# .net.

谢谢,
马拉里。

Thanks, Manali.

推荐答案

我知道这是一个老帖子,但别人还是会发现这很有用。这是相当优雅而简洁。我用这与.NET 4.0的DateTimePicker但早期版本应该以最小的调整工作。它利用为MinDate并检查

I know this is an older post, but others might still find this useful. It is fairly elegant and concise. I used this with a .Net 4.0 DateTimePicker but earlier versions should work with minimal tweaking. It leverages MinDate and Checked.

    // 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; 

这篇关于设置的DateTimePicker值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆