在單元格gridview中雙擊時發現錯誤,因為DTE列和數量列中有空白或空記錄。有沒有最好的解決方案或建議?注意:我使用 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 cellValue = DataGridView1.Rows(x).Cells(1).Value
DateTimePicker1.Value = CDate(If(cellValue Is Nothing OrElse cellValue Is DBNull.Value, String.Empty, cellValue.ToString()))
Dim cellValue1 = DataGridView1.Rows(x).Cells(2).Value
NumericUpDown1.Value = CDec(If(cellValue1 Is Nothing OrElse cellValue1 Is DBNull.Value, String.Empty, cellValue1.ToString()))
End Sub

uj5u.com熱心網友回復:
注意:這是未經測驗的代碼。在將值分配給控制元件之前,您需要先檢查 DTE 和 QTY 列是否不為空。
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
x = e.RowIndex
txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
Dim dt = DataGridView1.Rows(x).Cells(1).Value.ToString()
Dim qty = DataGridView1.Rows(x).Cells(2).Value.ToString()
' Check if DTE column is not empty
If dt <> nothing Then
DateTimePicker1.Value = Cdate(dt).Date
Else
' you can do something here
End If
' Check if QTY column is a number
If IsNumeric(qty) Then
NumericUpDown1.Value = qty
Else
NumericUpDown1.Value = 0
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448305.html
