Code/Tea/Etc...

Duncan Mackenzie has too much time on his hands

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!

Published Friday, February 28, 2003 2:03 PM by Duncanma

Comments

 

Eto said:

This is what I found as well.. but I got into a pickle, because for some unknown reason, binding to this ALWAYS makes it flag the DataRow as modified, so using HasChanges() will ALWAYS return true!.. I wanted to know if the user changed anything so I can prompt them about saving before exiting, so I found this variation to work (FINALLY!):

public class DbDateTimePicker : System.Windows.Forms.DateTimePicker
{
public DbDateTimePicker()
{
}

[Bindable(true), Browsable(false)]
public new object Value
{
get
{
if (Checked) return base.Value;
else return DBNull.Value;
}
set
{
if (Convert.IsDBNull(value)) this.Checked = false;
else base.Value = Convert.ToDateTime(value);
}
}
}
June 6, 2003 6:07 PM
 

TrackBack said:

Another little code snippet : Code/Tea/Etc...
February 28, 2003 2:03 PM
 

Kevin said:

both of your solution dont' seem to work for me. the db value did get binded, but won't unchecked when value is dbnull.

March 1, 2004 9:02 AM
 

Kevin said:

I think I found a perfect solution to it.
even with the modification with Eto, the picker still have problem, because the picker won't update itself when changing from unchecked to checked or vice versa, with little magic touch it will work.

public class DbDateTimePicker : System.Windows.Forms.DateTimePicker
{
public DbDateTimePicker()
{
}

[Bindable(true), Browsable(false)]
public new object Value
{
get
{
if (Checked) return base.Value;
else return DBNull.Value;
}
set
{
if (Convert.IsDBNull(value))
{
//the line below is the magic touch
this.Checked = true;
this.Checked = false;
}
else base.Value = Convert.ToDateTime(value);
}
}
}

it seems that the picker UI won't update unless it clearly change from Checked true to false.
March 1, 2004 9:39 AM
 

Duncan Mackenzie said:

Thanks Kevin, I'll have to try that... it is odd that it was necessary though, since I am using that control without any issues in my applications already. Perhaps there is some other difference in how I am binding and handling changes that avoids this problem?
March 1, 2004 9:43 AM
 

Alton Goerby said:

Just bind to DateTimePicker.Text and it will work; If DBNull is autoconverted to DateTime.Now;

;)
March 26, 2004 6:52 AM
 

Hugoski said:

The same solution of Kevin, but in Visual Basic .NET.

Option Explicit On

Imports System
Imports System.ComponentModel

Public Class DBDateTimePicker
Inherits System.Windows.Forms.DateTimePicker

#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

<Bindable(True), Browsable(True)> _
Public Shadows Property Value() As Object
Get
If (MyBase.Checked) Then
Return MyBase.Value
Else
Return DBNull.Value
End If
End Get
Set(ByVal Value As Object)
If IsDBNull(Value) Then
Me.Checked = True
Me.Checked = False
Else
MyBase.Value = Convert.ToDateTime(Value)
End If
End Set
End Property


End Class
April 10, 2004 10:23 PM
 

Magnus Aycox said:

The above solution just doesn't cut it in all situations. The checkbox doesn't respond correctly when going from a DBNull row to a non-DBNull row. Please find below the fixed code.

public class DBDateTimePicker : System.Windows.Forms.DateTimePicker
{
public DBDateTimePicker()
{
}

[Bindable(true), Browsable(false)]
public new object Value
{
get
{
if(Checked)
return(base.Value);
else
return(DBNull.Value);
}
set
{
if(System.Convert.IsDBNull(value))
{
base.Value = DateTime.Today; // Show todays date as default when greyed out
//the line below is the magic touch
this.Checked = true;
this.Checked = false;
}
else
{
this.Checked = true; // Set checked - just setting the Value property isn't enough
base.Value = Convert.ToDateTime(value);
}
}
}
}
May 27, 2004 5:54 AM
Anonymous comments are disabled

This Blog

Syndication

News

This blog has moved to my own VB site

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker