删除时间从日期/时间vb.net
问题描述:
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
textbox5是应该只显示日期,但当我点击按钮,文本框显示我的日期和时间。例如:12/01/12 12:00 AM。删除时间从日期/时间vb.net
我该如何删除显示在文本框中的时间?
答
的Try ...
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
答
有datetime格式的功能。检查这个http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.71).aspx
+0
我已经尝试过从该页面的例子,但它仍然无法正常工作 –
答
由于CurrentCells(4).value的是一个对象,尝试强制转换为DateTime
然后使用ToShortDateString Method
Form2.TextBox5.Text = CType(dgv1.CurrentRow.Cells(4).Value, DateTime).ToShortDateString
转换,也可以使用DateTime.TryParse
将返回true,如果转换为succesfull
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
答
我只是删除了的ToString在这条线:
Form2.TextBox5.Text = dgv1.CurrentRow.Cells(4).Value.ToString
当我除去的ToString它只是显示在文本框日期
短日期它显示一个错误:从字符串“MM/DD/YYYY” 转换到类型“整数”是无效的。 –
好的...什么是该单元格值的数据类型? – Basic
这是一个日期/时间类型,并使用访问作为数据库 –