Search code examples
sqlvb.netformssmalldatetimenewforms

Move smalldatetime from databound DGV to new form textbox and change format


I am currently working in windows form application in visual studio express for desktop. I also have an sql back end. I am trying to pull a smalldatetime from a DGV that has been loaded from an SQL table and then move it into a textbox on a new form. Right now the date format is MM/dd/yyyy HH:mm:ss. I need the datetime to be in the format yyyy-MM-dd HH:mm:ss. Here is my code:-

Try

      Dim f As New frmCuttingMachineCutList

      If e.ColumnIndex = 1 Then
        Dim Row_Index As Integer = DGVFinish.CurrentCell.RowIndex
        MsgBox(DGVFinish.Rows(Row_Index).Cells(5).Value)
        f.txtshear.Text = DGVFinish.Rows(Row_Index).Cells(5).ToString("yyyy-MM-dd HH:mm:ss")
        f.lblshear.Text = DGVFinish.Rows(Row_Index).Cells(1).Value

        End If


        f.Show()

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

Solution

  • you may need to convert your cell's value to a DateTime before being able to format it. Try

    Dim date as DateTime = DGVFinish.Rows(Row_Index).Cells(5).Value as DateTime;
    f.txtshear.Text = date.ToString("yyyy-MM-dd HH:mm:ss");