我正在尝试计算数据网格视图的总列,Oracle中的数据类型,空值的数据网格视图列.....
Dim X As Integer
Dim y As Integer
For line As Integer = 0 To dgvAccDetail.RowCount - 1
X = X + dgvAccDetail.Rows(line).Cells(4).Value
y = y + dgvAccDetail.Rows(line).Cells(5).Value
Next
TXTVoucherDTotal.Text = X
txtvoucherCtotal.Text = y
但是得到味精:
没有为类型'integer'和类型'dbnull'定义运算符'+'
.....请告诉我这段代码出了什么问题。
答案 0 :(得分:1)
这假定您将“ DBNull”视为0:
Dim X As Integer
Dim y As Integer
For line As Integer = 0 To dgvAccDetail.RowCount - 1
X = X + If(IsDBNull(dgvAccDetail.Rows(line).Cells(4).Value),0,dgvAccDetail.Rows(line).Cells(4).Value)
y = y + If(IsDBNull(dgvAccDetail.Rows(line).Cells(5).Value),0,dgvAccDetail.Rows(line).Cells(5).Value)
Next
TXTVoucherDTotal.Text = X.tostring
txtvoucherCtotal.Text = y.tostring
编辑:为避免在单元格中的值不是整数(而不是它将计为0)的情况下引发异常-正如Ctznkane525在评论中指出的那样。
Dim X As Integer = 0
Dim y As Integer = 0
For line As Integer = 0 To dgvAccDetail.RowCount - 1
If dgvAccDetail.Rows(line).Cells.Count >= 5 AndAlso TypeOf dgvAccDetail.Rows(line).Cells(4).Value Is Integer Then
X = X + DirectCast(dgvAccDetail.Rows(line).Cells(4).Value, Integer)
End If
If dgvAccDetail.Rows(line).Cells.Count >= 6 AndAlso TypeOf dgvAccDetail.Rows(line).Cells(5).Value Is Integer Then
y = y + DirectCast(dgvAccDetail.Rows(line).Cells(5).Value, Integer)
End If
Next
TXTVoucherDTotal.Text = X.ToString
txtvoucherCtotal.Text = y.ToString