2012-12-05 15 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 è quello che dovrebbe mostrare solo la data ma quando clicco sul pulsante, la casella di testo mostra la data e l'ora. Es: 12/01/12 12:00 AM.Rimuovi ora dalla data/ora vb.net

Come posso rimuovere l'ora dalla visualizzazione nella casella di testo?

risposta

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

Essa mostra un errore: conversione da stringa "mm/gg/aaaa" nel tipo 'integer' non è valido. –

+1

Ok ... Qual è il tipo di dati di quel valore di cella? – Basic

+0

È un tipo Data/Ora e sto usando l'accesso come database –

7

Dal momento che i CurrentCells (4) .Value è un oggetto Prova a trasmettere ad un DateTime poi convertire utilizzando il ToShortDateString Method

Form2.TextBox5.Text = CType(dgv1.CurrentRow.Cells(4).Value, DateTime).ToShortDateString 

oppure è possibile utilizzare DateTime.TryParse che restituirà true se la conversione è di successo

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

ho appena rimosso il .ToString in questa linea:

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

Quando ho rimosso il .ToString mostra solo la data breve in data casella di testo