从日期/时间vb.net删除时间

Rar*_*rar 7 vb.net datetime textbox winforms

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Form2.Show()

    Form2.TextBox1.Text = dgv1.CurrentRow.Cells(0).Value.ToString
    Form2.TextBox2.Text = dgv1.CurrentRow.Cells(1).Value.ToString
    Form2.TextBox3.Text = dgv1.CurrentRow.Cells(2).Value.ToString
    Form2.TextBox4.Text = dgv1.CurrentRow.Cells(3).Value.ToString
    Form2.TextBox5.Text = dgv1.CurrentRow.Cells(4).Value.ToString
    Form2.TextBox6.Text = dgv1.CurrentRow.Cells(5).Value.ToString
    Form2.TextBox7.Text = dgv1.CurrentRow.Cells(6).Value.ToString
Run Code Online (Sandbox Code Playgroud)

textbox5是应该只显示日期的那个,但是当我点击按钮时,文本框会显示日期和时间.例如:12/01/12 12:00 AM.

如何将时间从显示到文本框中删除?

Mar*_*all 8

由于CurrentCells(4).Value是一个对象,尝试将其DateTime转换为然后使用转换ToShortDateString Method

Form2.TextBox5.Text = CType(dgv1.CurrentRow.Cells(4).Value, DateTime).ToShortDateString
Run Code Online (Sandbox Code Playgroud)

或者DateTime.TryParse如果转换成功,您可以使用哪个将返回true

Dim tempDate As Date
If DateTime.TryParse(CStr(dgv.CurrentRow.Cells(4).Value), tempDate) Then
    Form2.TextBox5.Text = tempDate.ToShortDateString
Else
    Form2.TextBox5.Text = "Invalid Date"
End If
Run Code Online (Sandbox Code Playgroud)


Bas*_*sic 2

尝试...

If dgv1.CurrentRow.Cells(4).Value IsNot Nothing
    Form2.TextBox5.Text = String.Format("{0:dd/mm/YYYY}", dgv1.CurrentRow.Cells(4).Value)
Else
    Form2.TextBox5.Text = ""
End If
Run Code Online (Sandbox Code Playgroud)