文本框日期(txt.DTE)是否應該為空,而不是包含我雙擊單元格gridview的上一個日期的日期。有解決方案還是我的代碼錯了?
注意:我使用 Visual Studio 2010
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
Dim dte = DataGridView1.Rows(x).Cells(1).Value.ToString()
If dte <> Nothing Then
txtDTE.Text = CDate(dte)
End If
txtQTY.Text = DataGridView1.Rows(x).Cells(2).Value.ToString()
txtPRICE.Text = DataGridView1.Rows(x).Cells(3).Value.ToString()
End Sub

uj5u.com熱心網友回復:
在您的子句中添加else代碼if以清除txtDTE如果日期列為空的值。
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
Dim dte = DataGridView1.Rows(x).Cells(1).Value.ToString()
If dte <> Nothing Then
txtDTE.Text = CDate(dte)
else
' CLEAR txtDTE if dt = nothing
txtDTE.clear()
End If
txtQTY.Text = DataGridView1.Rows(x).Cells(2).Value.ToString()
txtPRICE.Text = DataGridView1.Rows(x).Cells(3).Value.ToString()
End Sub
另一個建議:
您可以使用e.RowIndex而不是DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)也回傳當前行索引。
例子:
x = e.RowIndex()
代替
x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/455651.html
