none
Date Time Picker and Data grid view Cell click RRS feed

  • Question

  • Hi Good People

    I have A DataGridView And when I click A cell in Any row, All the relevant Data gets put into textboxes Except the for DateTimePicker, The DateTimePicker Throws an Error Because The Date is in the Wrong Format?

    I tried Using A Textbox to get over this issue to no Avail, Because When I click a Cell in the DataGridView the textbox shows Date & Time ( 08/04/2018 16:00) when I only require just the Date.

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            'GetBooking()
    
            Dim index As Integer
            index = e.RowIndex
            Dim SelectedRow As DataGridViewRow
            SelectedRow = DataGridView1.Rows(index)
            'Column 0   BookingID
            txtBookingID.Text = SelectedRow.Cells(0).Value.ToString()
            'Column 1   PickupDate
            txtPickupDate.Text = SelectedRow.Cells(1).Value.ToString()
            'Column  2  PickupTime
    
    
            dtpPickupDate.Text = SelectedRow.Cells(1).Value.ToString()
    
    
            mtbPickupTime.Text = SelectedRow.Cells(2).Value.ToString()
            'Column  3  AccountType
            txtAccountType.Text = SelectedRow.Cells(3).Value.ToString()
            'Column 4 CustomerName
            txtCustomerName.Text = SelectedRow.Cells(4).Value.ToString()
            'Column  5  PickupAddress
            txtPickupAddress.Text = SelectedRow.Cells(5).Value.ToString()
            'Column 6   DropAddress
            txtDropAddress.Text = SelectedRow.Cells(6).Value.ToString()
            'Column  7 txtInsertFare
            txtInsertFare.Text = SelectedRow.Cells(7).Value.ToString()
            'Column 8 VehicleRequired
            txtVehicleRequired.Text = SelectedRow.Cells(8).Value.ToString()
            'Column 9  DriverCallsign
            txtDriverCallsign.Text = SelectedRow.Cells(9).Value.ToString()
            'Column 10 DriverName
            txtDriverName.Text = SelectedRow.Cells(10).Value.ToString()
            'Column 11 WeekNumber
            txtWeekNumber.Text = SelectedRow.Cells(11).Value.ToString()
            'Column 12 Notes
            txtNotes.Text = SelectedRow.Cells(12).Value.ToString()
            'Column 13 TakenBy
            txtTakenBy.Text = SelectedRow.Cells(13).Value.ToString()
            'Column 14 DateTimeInserted
            txtDateTimeInserted.Text = SelectedRow.Cells(14).Value.ToString()
            'Enable Button cmdAllocateDriver
            cmdAllocateDriver.Enabled = True
    
            'Dim D As Date
            'txtPickupDate.Text = D.ToShortDateString
        End Sub

    Can Anyone please help Me with this Issue

    Kind Regards 

    Gary


    Gary Simpson

    Sunday, April 8, 2018 3:11 PM

Answers

  • Hi

    When setting the Date of a DateTimePicker,use the .Value property. and set it with a Date value.

    For example, (assuming the DGV cell contains a valid  date string), dtpPickupDate.Value = CDate(SelectedRow.Cells(1).Value.ToString())


    Regards Les, Livingston, Scotland

    • Marked as answer by Gary Simpson Sunday, April 8, 2018 5:04 PM
    Sunday, April 8, 2018 3:42 PM
  • May This Help You

    If dtpPickupDate is a Datepicker then

    Check DatePicker's MinDate & MaxDate Properties

    E.g.

    dtpPickupDate.MaxDate = New Date(2018, 4, 8, 21, 20, 0, 0)

    Implies that dtpPickupDate can't have Date & Time Value> "08-04-2018 21:20"

    Best Regards
    Vikram Manjare
    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.

    • Marked as answer by Gary Simpson Sunday, April 8, 2018 5:04 PM
    Sunday, April 8, 2018 3:54 PM
  • I don't know how you assign the date value to the DateTimePicker to begin with, but if it came from a string value then you should validate first:

            Dim validDate As Date
            'hard-coded date to illustrate
            If DateTime.TryParse("06/04/2018 00:00:00", validDate) Then
                DateTimePicker1.Value = validDate
            Else
                'invalid date
            End If


    Paul ~~~~ Microsoft MVP (Visual Basic)


    Sunday, April 8, 2018 4:16 PM
  • Hi

    Did you check the MinDate and MaxDate properties of the DateTimePicker as Vikram suggested.

    The Exception shows a valid Date, so it has to be an issue with the DateTimePicker properties somewhere.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, April 8, 2018 4:18 PM
    • Marked as answer by Gary Simpson Sunday, April 8, 2018 5:04 PM
    Sunday, April 8, 2018 4:16 PM

All replies

  • Hi

    When setting the Date of a DateTimePicker,use the .Value property. and set it with a Date value.

    For example, (assuming the DGV cell contains a valid  date string), dtpPickupDate.Value = CDate(SelectedRow.Cells(1).Value.ToString())


    Regards Les, Livingston, Scotland

    • Marked as answer by Gary Simpson Sunday, April 8, 2018 5:04 PM
    Sunday, April 8, 2018 3:42 PM
  • May This Help You

    If dtpPickupDate is a Datepicker then

    Check DatePicker's MinDate & MaxDate Properties

    E.g.

    dtpPickupDate.MaxDate = New Date(2018, 4, 8, 21, 20, 0, 0)

    Implies that dtpPickupDate can't have Date & Time Value> "08-04-2018 21:20"

    Best Regards
    Vikram Manjare
    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.

    • Marked as answer by Gary Simpson Sunday, April 8, 2018 5:04 PM
    Sunday, April 8, 2018 3:54 PM
  • Hi

    When setting the Date of a DateTimePicker,use the .Value property. and set it with a Date value.

    For example, (assuming the DGV cell contains a valid  date string), dtpPickupDate.Value = CDate(SelectedRow.Cells(1).Value.ToString())


    Regards Les, Livingston, Scotland

    Hi Les 

    Thank you once again for getting back to me, I have used your Suggestion, And when I click a row in the DGV, Your code still produce an Error, But I might have some Properties set wrong. 

    In the DGV COLUMN The format of the date is dd MM yyyy

    I must be doing something wrong

    Regards 

    Gary


    Gary Simpson

    Sunday, April 8, 2018 4:12 PM
  • I don't know how you assign the date value to the DateTimePicker to begin with, but if it came from a string value then you should validate first:

            Dim validDate As Date
            'hard-coded date to illustrate
            If DateTime.TryParse("06/04/2018 00:00:00", validDate) Then
                DateTimePicker1.Value = validDate
            Else
                'invalid date
            End If


    Paul ~~~~ Microsoft MVP (Visual Basic)


    Sunday, April 8, 2018 4:16 PM
  • Hi

    Did you check the MinDate and MaxDate properties of the DateTimePicker as Vikram suggested.

    The Exception shows a valid Date, so it has to be an issue with the DateTimePicker properties somewhere.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, April 8, 2018 4:18 PM
    • Marked as answer by Gary Simpson Sunday, April 8, 2018 5:04 PM
    Sunday, April 8, 2018 4:16 PM
  • Hi Good People

    Thank you For your input's Everyone, Everything works fine that was on my issue Now.

    Regards

    Gary 


    Gary Simpson

    Sunday, April 8, 2018 5:08 PM