none
Having problem with DateTimePicker Control RRS feed

  • Question

  • Hi Guys,

    Once more, I need your inputs.

    I Build a window form with a DateTimePicker Control which is bound to a binding source in order to save the date value in a database. It is working fine but there are some issues when the date is programmatically set in my VB code. Let me explain.

    1. When I select the same date that is the same as the default displayed date, the code return me that the value of the date in null but it works when I select a date that is different from the displayed default date

    2. When I set programmatically the default date of the control (with either Text or Value property) which a known date that the user does not need to change, the code return me that the value of the date in null.

    It looks like the date value cannot be programmatically set but only entered interactively by the user.

    I hope you have some clues for me.

    Regards

    Ekoue.

    Monday, January 8, 2018 2:00 PM

Answers

  • Hi,

    From the different replies, I tried to bind the data fields to the databinding text property of the datetimepicker control instead of the value property. I got successful results.

    Many thanks

    • Marked as answer by Ekoue Tuesday, January 9, 2018 3:08 PM
    Tuesday, January 9, 2018 2:57 PM

All replies

  • Need to see some code.

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Monday, January 8, 2018 2:34 PM
  • Hi Ekoue,

    You said that  DateTimePicker Control which is bound to a binding source, If so then are you sure you have actually bound the date controls to the desired fields? If this is a binding issue then you might also want to explain or show how you have set up your binding and what objects you are using for that binding. I do one test at my side, and I don't get null value.

    Dim dt As New DataTable
            dt.Columns.Add("UserName", GetType(String))
            dt.Columns.Add("Age", GetType(DateTime))
            dt.Rows.Add("A", "2017-01-01")
            dt.Rows.Add("B", "2016-01-01")
            DateTimePicker1.DataBindings.Add("Value", dt, "Age")
            Console.WriteLine(DateTimePicker1.Value)

    I find one article that you could take a look:

    https://support.microsoft.com/en-us/help/313513/invalidcastexception-when-you-bind-datetimepicker-that-contains-a-null

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, January 10, 2018 9:49 AM
    Tuesday, January 9, 2018 7:28 AM
    Moderator
  • Hi,

    From the different replies, I tried to bind the data fields to the databinding text property of the datetimepicker control instead of the value property. I got successful results.

    Many thanks

    • Marked as answer by Ekoue Tuesday, January 9, 2018 3:08 PM
    Tuesday, January 9, 2018 2:57 PM