Results 1 to 11 of 11

Thread: [2.0] Binding to my NullableDateTimePicker control

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Question [2.0] Binding to my NullableDateTimePicker control

    With the addition of Nullable Types to C# 2.0, I wrote my own NullableDateTimePicker control in very few lines of code, as opposed to the nullable DateTimePicker controls I've found that were written for C# 1.1.


    csharp Code:
    1. public partial class NullableDateTimePicker : DateTimePicker
    2. {
    3.     private DateTime? dtNullable;
    4.  
    5.     public NullableDateTimePicker()
    6.     {
    7.         base.Format = DateTimePickerFormat.Custom;
    8.         base.CustomFormat = " ";
    9.     }
    10.  
    11.     public new DateTime? Value
    12.     {
    13.  
    14.         get
    15.         {
    16.             if (dtNullable.HasValue)
    17.                 return base.Value;
    18.  
    19.             return null;
    20.         }
    21.  
    22.         set
    23.         {
    24.             if (value == null)
    25.                 dtNullable = null;
    26.             else
    27.                 dtNullable = base.Value = value.Value;
    28.  
    29.             OnValueChanged(EventArgs.Empty);
    30.         }
    31.     }
    32.  
    33.     protected override void OnValueChanged(EventArgs eventargs)
    34.     {
    35.  
    36.         if (this.dtNullable.HasValue)
    37.         {
    38.             base.Format = DateTimePickerFormat.Short;
    39.         }
    40.         else
    41.         {
    42.             base.Format = DateTimePickerFormat.Custom;
    43.             base.CustomFormat = " ";
    44.         }
    45.  
    46.         base.OnValueChanged(eventargs);
    47.     }
    48.  
    49.     protected override void OnCloseUp(EventArgs eventargs)
    50.     {
    51.         if (Control.MouseButtons == MouseButtons.None)
    52.         {
    53.             this.Value = base.Value;
    54.         }
    55.  
    56.         base.OnCloseUp(eventargs);
    57.     }
    58. }

    Which is alright except if I bind the Value property and the binding source returns a DBNull.Value then I get the following error:

    error Code:
    1. Invalid cast from 'System.DateTime' to 'System.Nullable`

    How can I represent a DateTime so that it may contain a valid DateTime, and instead of null, DBNull.Value?
    Last edited by wey97; Oct 24th, 2007 at 08:26 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Binding to my NullableDateTimePicker control

    Where does this exception get thrown exactly?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Binding to my NullableDateTimePicker control

    Quote Originally Posted by jmcilhinney
    Where does this exception get thrown exactly?
    On the last line:


    csharp Code:
    1. Binding b = new Binding("Value", this.bindingSource1,
    2.     this.notesDataSet.StatusNotes.DateEnteredColumn.ColumnName);
    3.  
    4. this.nullableDateTimePicker1.DataBindings.Add(b);

  4. #4
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: [2.0] Binding to my NullableDateTimePicker control

    Change the Set code for the Value property to this:

    Code:
    set
            {
                if (value == null || value == DBNull.Value)
                    dtNullable = null;
                else
                    dtNullable = base.Value = value.Value;
    
                 OnValueChanged(EventArgs.Empty);
            }
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Binding to my NullableDateTimePicker control

    Quote Originally Posted by MetalKid
    Change the Set code for the Value property to this:

    Code:
    set
            {
                if (value == null || value == DBNull.Value)
                    dtNullable = null;
                else
                    dtNullable = base.Value = value.Value;
    
                 OnValueChanged(EventArgs.Empty);
            }
    Nope. That won't even begin to compile.

    DateTime? cannot be converted to DBNull.Value.

  6. #6
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: [2.0] Binding to my NullableDateTimePicker control

    Alright, what about this?

    Code:
    set
            {
                if (value == null || !value.HasValue)
                    dtNullable = null;
                else
                    dtNullable = base.Value = value.Value;
    
                 OnValueChanged(EventArgs.Empty);
            }
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Binding to my NullableDateTimePicker control

    Quote Originally Posted by MetalKid
    Alright, what about this?

    Code:
    set
            {
                if (value == null || !value.HasValue)
                    dtNullable = null;
                else
                    dtNullable = base.Value = value.Value;
    
                 OnValueChanged(EventArgs.Empty);
            }
    No!

    The Value property has to accept DBNull.Value to be able to bind to database nulls.

  8. #8
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: [2.0] Binding to my NullableDateTimePicker control

    There really isn't anything you can do to the control's code to fix this except make it take an object instead of that nullable type and then do some type checking in the set and get... I think you'd have to go thru datatable that comes back and change any values that are DBNull.Value to null...
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Binding to my NullableDateTimePicker control

    Quote Originally Posted by MetalKid
    There really isn't anything you can do to the control's code to fix this except make it take an object instead of that nullable type and then do some type checking in the set and get... I think you'd have to go thru datatable that comes back and change any values that are DBNull.Value to null...
    I've already done that. That's what most of the samples for C# 1.1 do. I hoped there was a better way with Nullable types in C# 2.0.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Question Re: [2.0] Binding to my NullableDateTimePicker control

    So I decided it would be easiest to let Value be an object and then handle casting from the UI. Actually, I don't even have to cast if I bind the "Value" of the control to a DataSet member. The control works fine, binds to DBNull OK and if a new date is chosen from the DateTimePicker everything is fine.

    Problem is, if I set the Value of the control in code, the date displays properly, but isn't updated in the DataSet. So if I call Update from the DataAdapter, the column isn't updated.

    Is there anything I'm missing to get it to update the DataSet if the Value is set in code?

    csharp Code:
    1. public partial class NullableDateTimePicker : DateTimePicker
    2. {
    3.     private object dtNullable;
    4.     private bool isFormatting;
    5.  
    6.     public NullableDateTimePicker()
    7.     {
    8.         base.Format = DateTimePickerFormat.Custom;
    9.         base.CustomFormat = " ";
    10.     }
    11.  
    12.     #region Properties
    13.  
    14.     public new object Value
    15.     {
    16.  
    17.         get
    18.         {
    19.             if(this.dtNullable != null)
    20.                 return base.Value;
    21.  
    22.             return DBNull.Value;
    23.         }
    24.  
    25.         set
    26.         {
    27.             if (!this.isFormatting)
    28.             {
    29.                 if (value == DBNull.Value)
    30.                 {
    31.                     dtNullable = null;
    32.                 }
    33.                 else
    34.                 {
    35.                     this.dtNullable = base.Value = Convert.ToDateTime(value);
    36.                 }
    37.             }
    38.  
    39.             this.OnValueChanged(EventArgs.Empty);
    40.         }
    41.     }
    42.  
    43.     #endregion
    44.  
    45.  
    46.     #region Methods
    47.  
    48.     #region Overrides
    49.  
    50.     protected override void OnValueChanged(EventArgs eventargs)
    51.     {
    52.         if (!this.isFormatting)
    53.         {
    54.             this.isFormatting = true;
    55.  
    56.             if (this.dtNullable != null)
    57.             {
    58.                 if (base.Format != DateTimePickerFormat.Short)
    59.                     base.Format = DateTimePickerFormat.Short;
    60.             }
    61.             else
    62.             {
    63.                 if (base.Format != DateTimePickerFormat.Custom)
    64.                     base.Format = DateTimePickerFormat.Custom;
    65.  
    66.                 base.CustomFormat = " ";
    67.             }
    68.         }
    69.  
    70.         this.isFormatting = false;
    71.  
    72.         base.OnValueChanged(eventargs);
    73.     }
    74.  
    75.     protected override void OnCloseUp(EventArgs eventargs)
    76.     {
    77.         if (Control.MouseButtons == MouseButtons.None)
    78.         {
    79.             this.Value = this.dtNullable = base.Value;
    80.         }
    81.  
    82.         base.OnCloseUp(eventargs);
    83.     }
    84.  
    85.     protected override void OnKeyUp(KeyEventArgs e)
    86.     {
    87.         if (e.KeyCode == Keys.Delete)
    88.         {
    89.             this.Value = DBNull.Value;
    90.         }
    91.  
    92.         base.OnKeyUp(e);
    93.     }
    94.     #endregion
    95.  
    96.     #endregion
    97. }

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Binding to my NullableDateTimePicker control

    More background info...

    I just discovered that if I set the Value of the NullableDateTimePicker through code, but click into the control to give it the focus, then remove the focus, the control updates properly. I hooked up a Parse event to the DataBinding to Value and the only time the Parse event fires is if the control gains and loses focus.

    I guess my only option is, after the Value is changed through code, to trick the control into thinking it gained and lost focus, which I can't seem to do. Even if I set the Focus through code and/or fire the OnMouseClickEvent, it still won't update. Only if I click in and out of the control.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width