2012-12-05 128 views
6
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

我該如何刪除顯示在文本框中的時間?

回答

2

的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 
+0

短日期它顯示一個錯誤:從字符串「MM/DD/YYYY」 轉換到類型「整數」是無效的。 –

+1

好的...什麼是該單元格值的數據類型? – Basic

+0

這是一個日期/時間類型,並使用訪問作爲數據庫 –

7

由於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 
1

我只是刪除了的ToString在這條線:

Form2.TextBox5.Text = dgv1.CurrentRow.Cells(4).Value.ToString 

當我除去的ToString它只是顯示在文本框日期