在执行NetValueValidation()时,我得到以下括号内的错误(操作符' - '未定义类型'Double'并键入'DBNull'。)。但是在执行DiscPerValidation()
后,它在“discper”列中工作Private Sub grdPurchase_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdPurchase.CellEndEdit
CellEdited = True
CurrentColumn = e.ColumnIndex
CurrentRow = e.RowIndex
If Me.grdPurchase.Columns(Me.grdPurchase.CurrentCell.ColumnIndex).Name = "itemcode" Then
If Not Me.grdPurchase.Rows(e.RowIndex).Cells("itemcode").Value Is DBNull.Value Then
SeekItemDetails()
End If
Else
If Me.grdPurchase.Columns(Me.grdPurchase.CurrentCell.ColumnIndex).Name = "packingratio" Then
PackingRatioValidation()
ElseIf Me.grdPurchase.Columns(Me.grdPurchase.CurrentCell.ColumnIndex).Name = "price" Then
PriceValidation()
NetValueValidation()
ElseIf Me.grdPurchase.Columns(Me.grdPurchase.CurrentCell.ColumnIndex).Name = "grossamt" Then
GrossAmtValidation()
'NetValueValidation()
ElseIf Me.grdPurchase.Columns(Me.grdPurchase.CurrentCell.ColumnIndex).Name = "kgs" Then
KGSValid()
'NetValueValidation()
ElseIf Me.grdPurchase.Columns(Me.grdPurchase.CurrentCell.ColumnIndex).Name = "discper" Then
DiscPerValidation()
'NetValueValidation()
End If
SendKeys.Send("{UP}" + "{TAB}")
End If
End Sub
Private Sub PackingRatioValidation()
If Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("packingratio").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("packingratio_temp").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("itemcode").Value Is DBNull.Value Then
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("qty").Value = _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("packingratio").Value * _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("packingratio_temp").Value
End If
End Sub
Private Sub PriceValidation()
If Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("itemcode").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("qty").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("price").Value Is DBNull.Value Then
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value = Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("price").Value * _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("qty").Value
End If
End Sub
Private Sub GrossAmtValidation()
If Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("itemcode").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("qty").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value Is DBNull.Value Then
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("price").Value = Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value / _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("qty").Value
End If
End Sub
Private Sub KGSValid()
If Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("itemcode").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("qty").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("price").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value Is DBNull.Value Then
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("kgrate").Value = Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value / _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("kgs").Value
End If
End Sub
Private Sub DiscPerValidation()
If Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("itemcode").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("discper").Value Is DBNull.Value Then
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("discamt").Value = _
(Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value / 100) * _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("discper").Value
End If
End Sub
Private Sub NetValueValidation()
If Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("itemcode").Value Is DBNull.Value And _
Not Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value Is DBNull.Value Then
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("netvalue").Value = _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value - _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("discamt").Value
End If
End Sub
答案 0 :(得分:2)
该消息表示在此代码中:
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("grossamt").Value - _
Me.grdPurchase.Rows(Me.grdPurchase.CurrentCell.RowIndex).Cells("discamt").Value
2个单元格中的一个是DBNull
。根据消息,它是一个参与减法的单元格,这就是这些2.奇怪的是,你在DBNull
的单元格之后测试单元格,而不是在这里。
不是没有,但是你有很多不必要的代码,这使得很难看到这种类型的东西。例如,CellEndEdit中的大块If可以简化为:
Dim name As String = dgv2.Rows(e.ColumnIndex).Cells(e.ColumnIndex).Value.ToString
Select Case name
Case "price"
PriceValidation(e.RowIndex)
NetValueValidation(...)
Case "grossamt"
GrossAmtValidation(...)
End Select
我不确定你为什么不使用可用的事件args。那么PriceValidation
所需要的就是这样的东西(将行传递给EventArgs
):
Private Sub PriceValidation(r As Int32)
' note: this is DOING pricing, not validation
Dim quan = SafeConvert(dgv.Rows(r).Cells("qty").Value)
Dim price = SafeConvert(dgv.Rows(r).Cells("price").Value)
dgv.Rows(r).Cells("grossamt").Value = (quan * price)
End Sub
我怀疑Option Strict
没有开启 - 但它应该是:
grdPurchase.Rows(...).Cells("qty").Value = _
grdPurchase.Rows(...).Cells("packingratio").Value * _
grdPurchase.Rows(...).Cells("packingratio_temp").Value
单元格值返回对象,而不是数字,因此代码尝试将2个对象相乘。您可以使用一个简单的过程来测试 value 单元格上的DBNull(以上在PriceValidation中使用):
' helper function to convert a DBNull
Private Function SafeConvert(v As Object) As Double
If System.DBNull.Value.Equals(v) Then
Return 0
Else
Dim d As Double = 0
If Double.TryParse(v.ToString, d) Then
End If
Return d
End If
End Function
它适用于具有数量或价格但不包含物料代码(文本)的单元格。每当你发现自己一遍又一遍地(或以上)输入相同的代码时,请停下来寻找更好的方法。