将DateTimePicker值设置为null

我正在开发一个带有两个DateTimePicker控件的WinForms UI。 最初,我想将控件的值设置为null,直到用户选择日期为止。 如果用户未选择日期,则会将值传递给数据库。

默认情况下,它采用当前日期。

你能不能提出一些建议,或者附上一段可以帮助我的代码。

我已经尝试设置MinDate属性,但它不起作用。

代码在C#.Net中。

谢谢,Manali。

我认为最好的解决方案是使用内置复选框,告诉用户是否指定了值。

设置控件属性ShowCheckBox = true

当你将值绑定到它时,执行类似的操作

  if (value == DateTime.MinValue) { datePicker.Checked = false; } else { datePicker.Checked = true; datePicker.Value = value; } 

读回值时,请Checked属性。

如果您在取消选中时不喜欢显示的值,则可以将其与其他建议结合使用。

  if (!datePicker.Checked) { // hide date value since it's not set datePicker.CustomFormat = " "; datePicker.Format = DateTimePickerFormat.Custom; } else { datePicker.CustomFormat = null; datePicker.Format = DateTimePickerFormat.Long; // set the date format you want. } 

我知道这是一篇较旧的post,但其他人可能仍然觉得这很有用。 它相当优雅和简洁。 我在.Net 4.0 DateTimePicker中使用它,但早期版本应该使用最小的调整。 它利用MinDate和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控件中显示空值,请在Designer.cs文件中进行以下更改:

 this.DateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom; this.DateTimePicker.CustomFormat = " "; 

当用户选择日期时,编码控件的ValueChanged属性并根据您的需要制作格式。 这将在控件中显示日期。

以上只显示UI中的控件为空白,但默认值仍为当前日期。

如果要将值设置/初始化为null或空字符串:

  1. 声明临时字符串变量说temp
  2. temp = DateTimePicker.CustomFormat.ToString();
  3. 检查temp是否为空字符串。 如果有这样的处理你喜欢。

这是我发现的最简单的解决方法。

可能需要使用TextBox的Appereance创建UserControl,当用户单击Calender Show时,当您选择Date时将其设置为TextBox。 它将允许空值。

DateTimePicker不允许null。

我希望这可以帮助你Link1 在选择器字段中显示空白

这也是Link2

假设dtpStartDate是您的DateTimePicker控件。

在Page_Load-中写下此代码

 `dtpStartDate.Format = DateTimePickerFormat.Custom; dtpStartDate.CustomFormat = " ";` 

在Value_Changed事件中写下此代码 –

 `startDate = Convert.ToString(dtpStartDate.Value); dtpStartDate.Format = DateTimePickerFormat.Short;` 

这里选择’short’作为格式。 您可以选择“长”和其他可用选项。

这是我使用的可空日期时间选择器的代码:

  Public Class DateTimePickerNullable Inherits DateTimePicker Public Shadows Event GotFocus(sender As Object, e As EventArgs) Public Shadows Event LostFocus(sender As Object, e As EventArgs) Private fvalue As DateTime? Private NoValueChange As Boolean = False Public Shadows Property Value As DateTime? Get Return fvalue End Get Set(value As DateTime?) fvalue = value If fvalue Is Nothing Then Me.Format = DateTimePickerFormat.Custom Me.CustomFormat = Space(Now.ToShortDateString.Length) NoValueChange = True MyBase.Value = Now NoValueChange = False Else Me.Format = DateTimePickerFormat.Short NoValueChange = True MyBase.Value = value.Value NoValueChange = False End If End Set End Property Protected Overrides Sub OnValueChanged(eventargs As EventArgs) If Not NoValueChange Then Value = MyBase.Value End If MyBase.OnValueChanged(eventargs) End Sub Protected Overrides Sub OnKeyDown(e As KeyEventArgs) If e.KeyCode = Keys.Delete Then Value = Nothing End If If Me.Format = DateTimePickerFormat.Custom Then Try If Char.IsNumber(ChrW(e.KeyCode)) Then If Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower.StartsWith("m") Then Value = New Date(Now.Year, CInt(ChrW(e.KeyCode).ToString), Now.Day) Else Value = New Date(Now.Year, Now.Month, CInt(ChrW(e.KeyCode).ToString)) End If SendKeys.Send(ChrW(e.KeyCode)) Exit Sub End If Catch End Try End If MyBase.OnKeyDown(e) End Sub Private HasFocus As Boolean = False Protected Overrides Sub OnGotFocus(e As EventArgs) MyBase.OnGotFocus(e) If Not HasFocus Then HasFocus = True RaiseEvent GotFocus(Me, New EventArgs) End If End Sub Protected Overrides Sub OnValidated(e As EventArgs) MyBase.OnValidated(e) HasFocus = False RaiseEvent LostFocus(Me, New EventArgs) End Sub End Class 
 dateTimePicker1.CustomFormat = " ";