绑定DateTimePicker控件VS NULL [英] bound DateTimePicker Control VS NULL
问题描述
好的,在数据绑定DTP上处理NULL异常的最佳方法是什么?
"没有它是NULL ...这就是为什么我把DTP放在那里所以我可以让它不是NULL!" ...啊
<预类= "prettyprint" 风格= ""> users_dtp_shiftstart.DataBindings.Add(QUOT;值QUOT ;, UsersBind," ShiftStart")
users_dtp_shiftend.DataBindings。添加(" value",UsersBind," ShiftEnd")
通常我在包含日期的数据表列上创建默认值,但现在我正在处理一个SQL表,该表具有NULL日期值的现有行。我不能只插入任何日期,如果日期不适用,列需要保持为NULL。
生活就像你今天要死了一样,学会好像你将要永远活着--Mahatma Gandhi
这可能会也可能不会解决null / nothing问题。
Public类DateTimePickerSpecial
继承System.Windows.Forms.DateTimePicker
私人oldFormat作为DateTimePickerFormat = DateTimePickerFormat.Short
私人oldCustomFormat作为字符串=没有
私人bIsNull由于布尔=假
公共事件ValueIsNothing(ByVal sender As Boolean)
Public Sub New()
MyBase.New()
End Sub
Public Shadows属性值()As DateTime
获取
如果bIsNull则
RaiseEvent ValueIsNothing(bIsNull)
返回DateTime.MinValue
Else
RaiseEvent ValueIsNothing(bIsNull)
返回MyBase.Value
结束如果
结束获取
设置(ByVal值)作为DateTime)
如果value = DateTime.MinValue那么
如果bIsNull = False那么
oldFormat = Me.Format
oldCustomFormat = Me.CustomFormat
bIsNull =真
结束如果
Me.Format = DateTimePickerFormat.Custom
Me.CustomFormat =" "
Else
如果bIsNull那么
Me.Format = oldFormat
Me.CustomFormat = oldCustomFormat
bIsNull = False
结束如果
MyBase.Value = value
结束如果
RaiseEvent ValueIsNothing(bIsNull)
结束集
结束属性
受保护覆盖Sub OnCloseUp(ByVal eventargs As EventArgs)
如果控制。 MouseButtons = MouseButtons.None然后
如果bIsNull那么
Me.Format = oldFormat
Me.CustomFormat = oldCustomFormat
bIsNull = False
结束如果
结束如果
的RaiseEvent ValueIsNothing(bIsNull)
MyBase.OnCloseUp(EventArgs的)
结束子
受保护的覆盖子的onkeydown(BYVALË作为KeyEventArgs )
MyBase.OnKeyDown(e)
如果e.KeyCode = Keys.Delete然后
Me.Value = DateTime.MinValue
End if
End Sub
End Class
Okay, what is the best way to handle the NULL exception on databound DTP??
"No duh it's NULL... it's why I put the DTP there so I can make it not NULL!" ...ugh
users_dtp_shiftstart.DataBindings.Add("value", UsersBind, "ShiftStart") users_dtp_shiftend.DataBindings.Add("value", UsersBind, "ShiftEnd")
Normally I create a default value on the datatable columns containing the date, but now I am dealing with a SQL table that has existing rows with NULL date values. I can't just insert any date either, the column needs to remain NULL if the date is not applicable.
Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi
This may or may not assist with the null/nothing issue.
Public Class DateTimePickerSpecial Inherits System.Windows.Forms.DateTimePicker Private oldFormat As DateTimePickerFormat = DateTimePickerFormat.Short Private oldCustomFormat As String = Nothing Private bIsNull As Boolean = False Public Event ValueIsNothing(ByVal sender As Boolean) Public Sub New() MyBase.New() End Sub Public Shadows Property Value() As DateTime Get If bIsNull Then RaiseEvent ValueIsNothing(bIsNull) Return DateTime.MinValue Else RaiseEvent ValueIsNothing(bIsNull) Return MyBase.Value End If End Get Set(ByVal value As DateTime) If value = DateTime.MinValue Then If bIsNull = False Then oldFormat = Me.Format oldCustomFormat = Me.CustomFormat bIsNull = True End If Me.Format = DateTimePickerFormat.Custom Me.CustomFormat = " " Else If bIsNull Then Me.Format = oldFormat Me.CustomFormat = oldCustomFormat bIsNull = False End If MyBase.Value = value End If RaiseEvent ValueIsNothing(bIsNull) End Set End Property Protected Overrides Sub OnCloseUp(ByVal eventargs As EventArgs) If Control.MouseButtons = MouseButtons.None Then If bIsNull Then Me.Format = oldFormat Me.CustomFormat = oldCustomFormat bIsNull = False End If End If RaiseEvent ValueIsNothing(bIsNull) MyBase.OnCloseUp(eventargs) End Sub Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs) MyBase.OnKeyDown(e) If e.KeyCode = Keys.Delete Then Me.Value = DateTime.MinValue End If End Sub End Class
这篇关于绑定DateTimePicker控件VS NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!