将 DateTimePicker 值设置为 null
我正在开发一个带有两个 DateTimePicker
控件的 WinForms UI。最初,我想将控件的值设置为 null,直到用户选择日期。如果用户没有选择日期,则会将 null 值传递给数据库。
默认情况下,它采用当前日期。
您能否提出一些建议或附加一段代码来帮助我。
我尝试设置 MinDate
属性,但它不起作用。
代码采用 C# .Net。
谢谢, 马纳里。
I am developing a WinForms UI with two DateTimePicker
controls. Initially I want to set the value of the controls to null until a user selects a date. If the user does not select a date, it will pass a value of null to the database.
By default it takes the current date.
Could you plz suggest something or attach a piece of code which will help me out.
I have tried setting the MinDate
property but it doesn't work.
The code is in C# .Net.
Thanks,
Manali.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道这是一篇较旧的文章,但其他人可能仍然觉得这很有用。它相当优雅和简洁。我将其与 .Net 4.0 DateTimePicker 一起使用,但早期版本应该只需进行最少的调整即可工作。它利用 MinDate 和 Checked。
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.
要在
DateTimePicker
控件中显示空值,请在Designer.cs
文件中进行以下更改:当用户选择日期时,在
ValueChanged
中编写代码code> 控件的属性并根据您的需要设置格式。这将在控件中显示日期。上面的控件在 UI 中仅显示为空白,但默认值仍然是当前日期。
如果要将值设置/初始化为 null 或空字符串:
temp = DateTimePicker.CustomFormat.ToString();
这是我发现的最简单的解决方法。
To display the null value in
DateTimePicker
control, Make the following changes in theDesigner.cs
file:When user selects a date, code-in the
ValueChanged
property of the control and make the format according to your needs. This will display the date in the control.The above one only display's the control as blank in UI, however the default value will still be the current date.
If you want to set/initialize the value as null or empty string:
temp = DateTimePicker.CustomFormat.ToString();
This is the easiest workaround I found.
假设 dtpStartDate 是您的 DateTimePicker 控件。
在 Page_Load 中写入此代码 -
在 Value_Changed 事件中写入此代码 -
这里选择“短”作为格式。您可以选择“长”和其他可用选项。
Let suppose
dtpStartDate
is your DateTimePicker control.Write this code in Page_Load-
Write this code in Value_Changed event-
here selected 'short' as Format. You can select 'Long' and other option available.
可能您需要创建一个具有文本框外观的用户控件,当用户单击日历显示时,当您选择日期时将其设置为文本框。它将允许空值。
DateTimePicker 不允许为 null。
May be you need to create a UserControl withe the Appereance of a TextBox,and when the user click on a Calender Show, when you select a Date set it to the TextBox. It will allow nulls values.
The DateTimePicker does not allow null.
我希望这对您有帮助 Link1 < em>要在选择器字段中显示空白
,这也是Link2
I hope this should help you Link1 To display blank in the picker field
and this too Link2
这是我使用的可为空日期时间选择器的代码:
This is the code of a nullable datetime picker that I use:
我认为最好的解决方案是使用内置复选框来告诉用户是否指定了值。
设置控件属性
ShowCheckBox = true
当您将值绑定到它时,请执行类似
当读回值时检查
Checked
属性的操作。如果您不喜欢未选中时显示的值,可以将其与其他建议结合起来。
I think the best solution is to use the build-in checkbox that tell the user if a value is specified or not.
Set the control property
ShowCheckBox = true
When you bind the value to it do something like
When reading back the value check the
Checked
property.If you don't like the displayed value when it's unchecked, you can combine that with the other suggestions.