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

时间:2015-08-06 13:16:32

标签: sql vb.net forms smalldatetime newforms

我目前正在使用visual studio express for windows桌面应用程序。我也有一个sql后端。我试图从已从SQL表加载的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

1 个答案:

答案 0 :(得分:1)

您可能需要先将单元格的值转换为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");