如何乘以列的所有值?

时间:2014-11-30 13:53:58

标签: vb.net datagridview

我喜欢这种布局的DataGridView:

enter image description here

我想找到一种方法来乘以列的所有值" Quota",特别是,我写了这样的代码:

    If MetroGrid1.Rows.Count > 0 Then
        If MetroGrid1.Columns.Contains("Quota") Then
            Dim CostTotal As Decimal = MetroGrid1.Rows.Cast(Of DataGridViewRow) _
                                       .Select(
                                        Function(x)
                                            Return CDec(x.Cells("Quota").Value)
                                        End Function
            ).Sum
            Dim risultato = CostTotal * giocata

问题是可用的唯一模式是总和和平均值,但没有乘法。我想找到一种方法来替换.Sum一些允许我在必要时多次或完全改变算法的命令。

1 个答案:

答案 0 :(得分:0)

要在遍历网格时乘以每个单元格,您可以循环遍历值并将其添加到变量中......首先,我们将确保存在值,如果是这样,请尝试将该值转换为双。如果是这样,则将旧值与新值相乘,等等......

 Dim dblTotal As Double = 0
 Dim dblValue As Double = 0


 For i As Integer = 0 To MetroGrid1.Rows.Count - 1
    If MetroGrid1.Rows(i).Cells("Quota").Value IsNot DBNull.Value Then
     If Double.TryParse(MetroGrid1.Rows(i).Cells("Quota").Value.ToString, dblValue) Then
      dblTotal *= dblValue
     End If
    End If

 Next