DateTimePicker and DBNull



You can’t bind a Windows Forms DateTimePicker to a field
that might contain DBNull… so I did this;


 

      public class
DBDateTimePicker:DateTimePicker

     
{

 

           
public
DBDateTimePicker()

           
{

                 
//

                 
// TODO: Add constructor logic
here

                 
//

           
}

 

       
public object DBValue

       
{

           
get

           
{

               
if (this.Checked)

                   
return base.Value;

               
else

                   
return
System.DBNull.Value;

           
}

           
set

           
{

               
if (System.Convert.IsDBNull(value))

                    this.Checked=false;

               
else

                   
this.Value = Convert.ToDateTime(value);

           
}

       
}

     
}

 

Then I bind to “DBValue”  (instead of Value) and it appears to
work fine… if it is null, it is unchecked and disabled, otherwise it is enabled
and can be set to any normal date value… if you uncheck the box yourself, then
the data field is set to DBNull…

 

Not sure if it the best idea, but I can’t override Value so this
seems like a reasonable alternative… of course, I never looked around for the
“official” solution or any other possible answers, so let me know if you have a
better idea!

DateTimePicker and DBNull

8 Responses

  1. I amended your source to remove the necessity to use the “Checked” property of the ‘Picker.
    This means that you are not compelled to use the checkbox in the ‘Picker if you don’t want to.
    Also a Null Date now displays blank text in the picker instead of a greyed out date.

    Let me know what you think.

    Regards,

    Baraholka

    ‘Original Author: Duncan MacKenzie
    ‘Original Source: http://www.dotnet247.com/247reference/a.aspx?u=http://blogs.msdn.com/duncanma/archive/2003/02/28/3242.aspx
    ‘Modifications
    ‘Date Who Description
    ‘——————————–
    ’01-OCT-2004 CKS Modified original source
    ‘ 1. No longer uses Checked property. This means ShowCheckBox property does not have to be set for successful use
    ‘ 2. Null date shows blank text instead of greyed out date. This utilises the technique shown by
    ‘ Pham Minh Tri http://www.thecodeproject.com/cs/miscctrl/Nullable_DateTimePicker.asp
    Option Explicit On

    Imports System
    Imports System.ComponentModel

    Public Class DBDateTimePicker
    Inherits System.Windows.Forms.DateTimePicker

    ‘Flag: tells us if Picker currently showing null value or not
    Private bIsNull As Boolean = False
    ‘Flag: Show null date in Picker
    Private bReturnNull As Boolean = False
    ‘Allows swapping between Custom format and original format of Picker
    Private oldFormat As DateTimePickerFormat

    #Region ” Windows Form Designer generated code ”

    Public Sub New()
    MyBase.New()

    ‘This call is required by the Windows Form Designer.
    InitializeComponent()

    ‘Add any initialization after the InitializeComponent() call

    End Sub

    ‘UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    ‘Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    ‘NOTE: The following procedure is required by the Windows Form Designer
    ‘It can be modified using the Windows Form Designer.
    ‘Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    components = New System.ComponentModel.Container
    End Sub

    #End Region

    Private Sub SetDBValue(ByVal Value As Object)
    If IsDBNull(Value) Then
    Me.bReturnNull = True
    ‘Show null date as space.
    ‘Check if Picker currently showing a Null Date
    If bIsNull = False Then
    Me.oldFormat = Me.Format
    Me.Format = DateTimePickerFormat.Custom
    Me.CustomFormat = ” ”
    bIsNull = True
    End If
    Else
    Me.bReturnNull = False
    MyBase.Value = Convert.ToDateTime(Value)
    ‘Date is not null. Restore original date Format
    If bIsNull Then
    Me.Format = Me.oldFormat
    Me.CustomFormat = Nothing
    bIsNull = False
    End If
    End If
    End Sub

    <Bindable(True), Browsable(True)> _
    Public Shadows Property DBValue() As Object
    Get
    If bReturnNull Then
    Return DBNull.Value
    Else
    Return MyBase.Value
    End If
    End Get
    Set(ByVal Value As Object)
    Me.SetDBValue(Value)
    End Set
    End Property

    Private Sub DBDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ValueChanged
    ‘Immediately permit display of changed date
    Me.SetDBValue(Value)
    End Sub
    End Class

    Baraholka October 1, 2004 at 6:26 am #
  2. oops here’s the binding implimentation:
    Me.pnlCustomerInformation.dtpBirthDateWithNull.DataBindings.Clear()
    Me.pnlCustomerInformation.dtpBirthDateWithNull.DataBindings.Add(New Binding(“Value”, e.Customer, “BirthDate”))

    the e.Customer is custom event. (fyi)

    Richard Basque August 19, 2005 at 6:51 pm #
  3. Hi,
    The code posted my Baraholka didn’t seem to work properly. The Format should always be DateTimePickerFormat.Custom (so leave it alone) and its only the CustomFormat property that needs to be change. Also this is a string. Here’s what worked for me.

    ‘Original Author: Duncan MacKenzie
    ‘Original Source: http://www.dotnet247.com/247reference/a.aspx?u=http://blogs.msdn.com/duncanma/archive/2003/02/28/3242.aspx
    ‘Modifications
    ‘Date Who Description
    ‘——————————–
    ’01-OCT-2004 CKS Modified original source
    ‘ 1. No longer uses Checked property. This means ShowCheckBox property does not have to be set for successful use
    ‘ 2. Null date shows blank text instead of greyed out date. This utilises the technique shown by
    ‘ Pham Minh Tri http://www.thecodeproject.com/cs/miscctrl/Nullable_DateTimePicker.asp
    ‘——————————-
    ‘Nullable and bindable DateTimePicker
    ‘Copied from http://blogs.duncanmackenzie.net/duncanma/archive/2003/02/28/152.aspx
    ‘Modified: 9/30/2004 11:26 PM by Baraholka
    ‘Modified again by Richard Basque
    ‘August 18, 2005
    ‘1. Shadowed the Value Property (slight modification)
    ‘2. Fixed the machanics. Didn’t seem to work for me. This one takes nulls and binds to the Value property.

    Option Explicit On

    Imports System
    Imports System.ComponentModel

    Public Class DBDateTimePicker
    Inherits System.Windows.Forms.DateTimePicker

    ‘Flag: tells us if Picker currently showing null value or not
    Private bIsNull As Boolean = False
    ‘Flag: Show null date in Picker
    Private bReturnNull As Boolean = False
    ‘Allows swapping between Custom format and original format of Picker
    Private oldCustomFormat As String ‘ DateTimePickerFormat

    #Region ” Windows Form Designer generated code ”

    Public Sub New()
    MyBase.New()

    ‘This call is required by the Windows Form Designer.
    InitializeComponent()
    ‘Add any initialization after the InitializeComponent() call

    End Sub

    ‘UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    ‘Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    ‘NOTE: The following procedure is required by the Windows Form Designer
    ‘It can be modified using the Windows Form Designer.
    ‘Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    components = New System.ComponentModel.Container
    End Sub

    #End Region

    Private Sub SetDBValue(ByVal _Value As Object)
    If IsDBNull(_Value) Then
    Me.bReturnNull = True
    ‘Show null date as space.
    ‘Check if Picker currently showing a Null Date
    If bIsNull = False Then
    Me.oldCustomFormat = Me.CustomFormat
    Me.CustomFormat = ” ”
    Me.Value = Now ‘set a default
    bIsNull = True
    End If
    Else
    Me.bReturnNull = False
    MyBase.Value = Convert.ToDateTime(_Value)
    ‘Date is not null. Restore original date Format
    If bIsNull Then
    Me.CustomFormat = oldCustomFormat
    bIsNull = False
    End If
    End If
    End Sub

    <Bindable(True), Browsable(True)> _
    Public Shadows Property Value() As Object
    Get
    If bReturnNull Then
    Return DBNull.Value
    Else
    Return MyBase.Value
    End If
    End Get
    Set(ByVal Value As Object)
    Me.SetDBValue(Value)
    End Set
    End Property

    Private Sub DBDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ValueChanged
    ‘Immediately permit display of changed date
    Me.SetDBValue(CType(sender, DateTimePicker).Value)
    End Sub

    End Class

    Richard Basque August 19, 2005 at 6:47 pm #
  4. Good article and site. Congratulations

    budowa domów July 4, 2007 at 4:43 pm #
  5. Good jobs.Thanks.

    sklep rowerowy August 2, 2007 at 11:40 am #
  6. I just added

    this.Checked=true;

    to the last else statement and it works perfectly.

    There are people all over the web struggling with this and the solution is so simple.

    MASSIVE Thanks.

    Damien Smith August 28, 2007 at 8:52 am #
  7. Thanks for this article. I like your posts

    meble biurowe September 17, 2007 at 2:29 pm #
  8. Worked fine for me! Unfortunately, when I had my laptop reformatted, I lost everything. Now I did the same thing but how come it didn’t work well for me?

    portraits art October 26, 2007 at 6:49 am #

Leave a Reply