将 smalldatetime 从数据绑定 DGV 移动到新表单文本框并更改格式

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

我目前正在 windows 桌面版 visual studio express 表单应用程序中工作。我还有一个 sql 后端。我正在尝试从从 SQL table 加载的 DGV 中提取一个 smalldatetime,然后将其移动到新表单的文本框中。现在日期格式是 MM/dd/yyyy HH:mm:ss。我需要日期时间的格式为 yyyy-MM-dd HH:mm:ss。这是我的代码:-

尝试

      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

您可能需要先将单元格的值转换为 DateTime,然后才能对其进行格式化。尝试

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