none
Can you set DateTimePicker value to Blank? RRS feed

  • Question

  • Good day all,

    I have a DateTimePicker that set the column Lead Time on my DataSet Table.
    Since it is DateTimePicker, it drops down a calendar for user to choose.

    The thing is, my client ask if they can set the value to blank, if they have not decided on the date to input.
    I'm not sure if it can be done since the DateTimePicker forces user to choose a date.

    Database is expecting a DateTime, how do I give it an empty string?

    Please advise the best way to do this.
    Thanks





    • Edited by wk89 Sunday, August 4, 2019 9:52 AM
    Sunday, August 4, 2019 9:38 AM

Answers

  • Hello,

    As I see it there is no straightforward method, the most commonly used logic will set the DataTimePicker Format to an single space then when the value changes (ValueChanged) event or CloseUp event change the format.

    So for a conceptual example

    Perhaps in form load (could be another event)

    DateTimePicker1.Format = DateTimePickerFormat.Custom
    DateTimePicker1.CustomFormat = " "

    So now the user must use the dropdown so we use

    Private Sub DateTimePicker1_CloseUp(sender As Object, e As EventArgs) _
        Handles DateTimePicker1.CloseUp
    
        DateTimePicker1.Format = DateTimePickerFormat.Long
    
    End Sub

    One issue with this if the do drop down and not select a date, the current date is now displayed. Another option with the same behavior is to use the check box.

    DateTimePicker2.Format = DateTimePickerFormat.Custom
    DateTimePicker2.CustomFormat = " "
    
    AddHandler DateTimePicker2.ValueChanged, Sub(s, ee)
                                                 DateTimePicker2.CustomFormat = If(DateTimePicker2.Checked AndAlso DateTimePicker2.Value <> DateTimePicker1.MinDate, "MM/dd/yyyy", " ")
                                             End Sub
    If the rule is they can't select today then you can have validation to prevent this but if the rule is today is okay then you should be fine.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wk89 Monday, August 5, 2019 2:43 AM
    Sunday, August 4, 2019 11:30 AM
    Moderator

All replies

  • Hello,

    As I see it there is no straightforward method, the most commonly used logic will set the DataTimePicker Format to an single space then when the value changes (ValueChanged) event or CloseUp event change the format.

    So for a conceptual example

    Perhaps in form load (could be another event)

    DateTimePicker1.Format = DateTimePickerFormat.Custom
    DateTimePicker1.CustomFormat = " "

    So now the user must use the dropdown so we use

    Private Sub DateTimePicker1_CloseUp(sender As Object, e As EventArgs) _
        Handles DateTimePicker1.CloseUp
    
        DateTimePicker1.Format = DateTimePickerFormat.Long
    
    End Sub

    One issue with this if the do drop down and not select a date, the current date is now displayed. Another option with the same behavior is to use the check box.

    DateTimePicker2.Format = DateTimePickerFormat.Custom
    DateTimePicker2.CustomFormat = " "
    
    AddHandler DateTimePicker2.ValueChanged, Sub(s, ee)
                                                 DateTimePicker2.CustomFormat = If(DateTimePicker2.Checked AndAlso DateTimePicker2.Value <> DateTimePicker1.MinDate, "MM/dd/yyyy", " ")
                                             End Sub
    If the rule is they can't select today then you can have validation to prevent this but if the rule is today is okay then you should be fine.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wk89 Monday, August 5, 2019 2:43 AM
    Sunday, August 4, 2019 11:30 AM
    Moderator
  • Okay, for a DataGridView there is a C# custom DataGridView column to handle NULL/Nothing dates, it's not perfect but pretty close.If you want to try it I did a conversion to VB.NET and can upload and provide a link to the classes.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 4, 2019 11:39 AM
    Moderator
  • Thank you!
    Monday, August 5, 2019 2:44 AM