Mahmoud Abdelaziz
Mahmoud Abdelaziz

Reputation: 1

How to transfer Time format "hh:mm tt" from Datagridview data cell to a datetimepicker?

I tried to do the following:where StartTime EndTime are a Datetimepicker

   StartTime.Value = "01/01/2010 " & DGVEvemts(1, DGVEvemts.Rows.Count - 1).ToString("hh:mm tt")

    EndTime.Value = "01/01/2010 " & DGVEvemts(2, DGVEvemts.Rows.Count - 1).ToString("hh:mm tt")

Upvotes: 0

Views: 94

Answers (2)

Mahmoud Abdelaziz
Mahmoud Abdelaziz

Reputation: 1

    If DGVEvemts.Rows.Count < 1 Then Return
   
    RecStTxt.Text = "01/01/2010 " & DGVEvemts(1, DGVEvemts.Rows.Count - 1).Value.ToString()
    RecEndTxt.Text = "01/01/2010 " & DGVEvemts(2, DGVEvemts.Rows.Count - 1).Value.ToString()
    StartTime.Value = RecStTxt.Text
    EndTime.Value = RecEndTxt.Text
    

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54417

You should not be using Strings at all. DO NOT convert between DateTime and String for any reason that doesn't involve requiring a String as an output, e.g. display or serialisation, or as an input, e.g. reading a file or user input at the console. If you are starting with a DateTime and you want to end with a DateTime then there should be no intermediate Strings involved.

Assuming that your DataGridView contains DateTime values, you can do this:

StartTime.Value = #1/01/2010# + CDate(DGVEvemts(1, DGVEvemts.Rows.Count - 1)).TimeOfDay

That creates a Date literal, gets the Date from the grid, gets the time portion from that Date and adds it to the literal. If the time in the grid might contain partial minutes and you only want whole minutes, you can do this:

Dim dt = CDate(DGVEvemts(1, DGVEvemts.Rows.Count - 1))

StartTime.Value = #1/01/2010# + New TimeSpan(dt.Hours, dt.Minutes, 0)

Upvotes: 2

Related Questions