Vb.net 如果datagridview中的列不可见,则计算总值

Vb.net 如果datagridview中的列不可见,则计算总值,vb.net,datagridview,Vb.net,Datagridview,我得到这个错误“没有为类型“Double”和类型“DBNull”定义运算符“+” 如果这两列不可见,如何获取列Inc和列Dec的总数?您可以在数据源上运行计算。如果基础源是数据表,则可以枚举数据行或使用表。计算访问基础数据行 For i As Integer = 0 To DataGridView1.RowCount - 1 fundwith += DataGridView1.Rows(i).Cells("Withdrawal").Value trans +=

我得到这个错误“没有为类型“Double”和类型“DBNull”定义运算符“+”


如果这两列不可见,如何获取列Inc和列Dec的总数?

您可以在数据源上运行计算。如果基础源是数据表,则可以枚举数据行或使用
表。计算

访问基础
数据行

 For i As Integer = 0 To DataGridView1.RowCount - 1
        fundwith += DataGridView1.Rows(i).Cells("Withdrawal").Value
        trans += DataGridView1.Rows(i).Cells("Trans").Value
        meal += DataGridView1.Rows(i).Cells("Meal").Value
        rep += DataGridView1.Rows(i).Cells("Rep").Value
        meet += DataGridView1.Rows(i).Cells("Meet").Value
        misc += DataGridView1.Rows(i).Cells("Misc").Value
        oth += DataGridView1.Rows(i).Cells("Others").Value
        inc += DataGridView1.Rows(i).Cells("Inc").Value
        dec += DataGridView1.Rows(i).Cells("Dec").Value
    Next
然后


inc
&
dec
列是否确实包含数据但不可见,或者这些列上可能只包含DBNull?谢谢。现在我知道我的错误了。该值为null,这就是我得到错误的原因。我认为您可以检查DBNull并自动将其转换为
Double
的默认值(0),类似于
inc+=CheckDBNull(DataGridView1.Rows(i).Cells(“inc”).value)
。虽然这肯定没有错,实际上不需要从
DataRowView
访问
DataRowView
,因为
DataRowView
可以提供相同的信息。
Dim drw as DataRow = DirectCast(DataGridView1.Rows(i).DataBoundItem, DataRowView).Row
fundwith += CDec(drw("Withdrawal")) 'replace CDec with whatever type conversion you need