如何将一列的所有值相乘? [英] How to multiply all the values of a column?
问题描述
我有一个像这样的DataGridView布局:
I've a DataGridView like this layout:
我想找到一种方法来乘以"Quota"列的所有值,特别是,我编写了这样的代码:
I would like to find a way to multiply all values of the column "Quota", in particular, I wrote a code like this:
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
的方法,该命令可以在必要时让我乘以或完全改变算法.
The problem is that the only mode available is the sum and average, but there is no multiplication. I would like to find a way to replace .Sum
with some command that allows me to multiply or totally change the algorithm if necessary.
推荐答案
要在遍历网格时乘以每个单元格,可以通过将值相乘并将其添加到变量中来循环...首先,我们要确保存在是一个值,如果是这样,请尝试将该值转换为双精度值.如果是这样,则将旧值乘以新值,依此类推...
To multiply every cell as you go through the grid you can loop through multiplying the values and adding it to a variable... First we'll make sure there is a value and if so lets try and cast that value as a double. If so multiply the old values with the new one and so on...
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
这篇关于如何将一列的所有值相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!