none
how to Retrieving a DateTime value is Null to DateTimePicker use my example ?

    Question

  • my example :

    Dim row As DataGridViewRow = DataGridView1.CurrentRow If row.Cells(2).Value IsNot Nothing AndAlso row.Cells(2).Value <> DBNull.Value Then  DateTimePicker6.Value = row.Cells(2).Value.ToString()

    Else

     'nothing' End If






    • Edited by srajmuneer Thursday, April 27, 2017 8:00 AM
    Thursday, April 27, 2017 6:06 AM

Answers

  • Hi srajmuneer,

    Please refer to the code below.

    Private Sub fun()
            Dim row As DataGridViewRow = DataGridView1.CurrentRow
    
            If row.Cells(2).Value IsNot Nothing AndAlso row.Cells(2).Value <> DBNull.Value Then
                DateTimePicker1.Format = DateTimePickerFormat.Custom
                DateTimePicker1.CustomFormat = "yyyy-MM-dd"
                DateTimePicker1.Value = Convert.ToDateTime(row.Cells(2).Value)
                DateTimePicker1.Enabled = True
    
            Else
                DateTimePicker1.Format = DateTimePickerFormat.Custom
                DateTimePicker1.CustomFormat = " "
                DateTimePicker1.Value = Date.FromOADate(0)
                DateTimePicker1.Enabled = False
                'nothing' 
            End If
        End Sub

    Best Regards,

    Cherry Bu


    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.

    Thursday, April 27, 2017 8:26 AM
    Moderator

All replies

  • Hi srajmuneer,

    Please refer to the code below.

    For i = 0 To dt.Rows.Count - 1
                If dt(i)("ColumnDate") IsNot Nothing AndAlso dt(i)("ColumnDate") IsNot DBNull.Value Then
    
                    DateTimePicker1.Format = DateTimePickerFormat.Custom
                    DateTimePicker1.CustomFormat = "yyyy-MM-dd"
                    DateTimePicker1.Value = Convert.ToDateTime(dt(i)("ColumnDate"))
                    DateTimePicker1.Enabled = True
    
    
                Else
                    DateTimePicker1.Format = DateTimePickerFormat.Custom
                    DateTimePicker1.CustomFormat = " "
                    DateTimePicker1.Value = Date.FromOADate(0)
                    DateTimePicker1.Enabled = False
                End If
            Next

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    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.

    Thursday, April 27, 2017 7:10 AM
    Moderator
  • You cannot set a DateTimePicker to Null (in fact beside a mistake from the development team a not used word in VB)

    However, you can set it to the default value, take care of the = operator. 

    Dt.Value = Nothing

    Be also aware that DBNull.Value is not a Null value, it is the content of a Null column in a DataBase Server, which is mostly simple empty. 


    Success
    Cor


    Thursday, April 27, 2017 8:01 AM
  • thanks Cherry Bu , can use my example with DBNILL in line code :

    DateTimePicker6.Value = row.Cells(2).Value.ToString()


    can use your line code with my line code :

    Dim row As DataGridViewRow = DataGridView1.CurrentRow
    
    DateTimePicker1.Value = Convert.ToDateTime(dt(i)("ColumnDate"))

    and

    DateTimePicker1.Value = Date.FromOADate(0)



    • Edited by srajmuneer Thursday, April 27, 2017 8:23 AM
    Thursday, April 27, 2017 8:03 AM
  • Hi srajmuneer,

    Please refer to the code below.

    Private Sub fun()
            Dim row As DataGridViewRow = DataGridView1.CurrentRow
    
            If row.Cells(2).Value IsNot Nothing AndAlso row.Cells(2).Value <> DBNull.Value Then
                DateTimePicker1.Format = DateTimePickerFormat.Custom
                DateTimePicker1.CustomFormat = "yyyy-MM-dd"
                DateTimePicker1.Value = Convert.ToDateTime(row.Cells(2).Value)
                DateTimePicker1.Enabled = True
    
            Else
                DateTimePicker1.Format = DateTimePickerFormat.Custom
                DateTimePicker1.CustomFormat = " "
                DateTimePicker1.Value = Date.FromOADate(0)
                DateTimePicker1.Enabled = False
                'nothing' 
            End If
        End Sub

    Best Regards,

    Cherry Bu


    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.

    Thursday, April 27, 2017 8:26 AM
    Moderator