如何执行循环以计算DataGridView中的单元格值?

时间:2018-09-25 16:09:24

标签: vb.net

如何将row的{​​{1}}的值与另一个column中的所有值相乘,然后对所有column进行循环。然后,最后,将每个rows中的值相加?

这里是一个例子。
请注意,我不需要具有或显示这三列(I,II,III)。我只是将它们放在其中以显示步骤。

非常感谢您的帮助!

Example

该循环仅适用于第3列的前两行。

row

1 个答案:

答案 0 :(得分:1)

此解决方案不使用DataGridView(或GridView),而是使用数组。技巧是创建行,列和乘法结果的列表。请参阅下表以查看列表的外观。创建列表后,它只是按第二个值(行)分组并计算第三个值的总和。

Dim C1 = { 40, 30, 20, 10 } ' column C1
Dim C2 = { 1, 2, 3 }        ' column C2

' to store the result of multiplication between C1 and C2
' first value is Column, second value is Row, third column is the multiplication result
Dim list = New List(Of Tuple(Of Integer, Integer, Integer))

For i = 0 To C2.Length - 1
    For j = 0 To C1.Length - 1
        list.Add(New Tuple(Of Integer, Integer, Integer)( i, i + j, C2(i) * C1(j) )) ' column, row, value
    Next
Next

' to store sum of each row
' key is row, value is sum of the row
Dim dict = New Dictionary(Of Integer, Integer)

For Each row In list ' iterate each row in list
    If dict.ContainsKey(row.Item2) ' if dictionary contains row number
        dict(row.Item2) += row.Item3 ' add value to existing row
    Else
        dict.Add(row.Item2, row.Item3) ' add new row
    End If
Next

For Each entry In dict
    Console.WriteLine("Total Row {0} = {1}", entry.Key, entry.Value)
Next

或者使用LINQ来获取总和。

Dim C1 = { 40, 30, 20, 10 } ' column C1
Dim C2 = { 1, 2, 3 }        ' column C2

' to store the result of multiplication between C1 and C2
' first value is Column, second value is Row, third column is the multiplication result
Dim list = New List(Of Tuple(Of Integer, Integer, Integer))

For i = 0 To C2.Length - 1
    For j = 0 To C1.Length - 1
        list.Add(New Tuple(Of Integer, Integer, Integer)( i, i + j, C2(i) * C1(j) )) ' column, row, value
    Next
Next

' LINQ sum
Dim result = From l In list
             Group By l.Item2  ' group by row
             Into Sum(l.Item3) ' sum of value

For Each row In result
    Console.WriteLine("Total Row {0} = {1}", row.Item2, row.Sum)
Next

enter image description here

列表的电子表格版本,其中彩色行按“行(第二个)”列分组。 enter image description here

结果:

Total Row 0 = 40
Total Row 1 = 110
Total Row 2 = 200
Total Row 3 = 140
Total Row 4 = 80
Total Row 5 = 30

希望您能在项目中实现此代码。

编辑。经过优化的解决方案,减少了循环。

Dim C1 = { 40, 30, 20, 10 } ' column C1
Dim C2 = { 1, 2, 3 }        ' column C2

Dim dict = New Dictionary(Of Integer, Integer)

For i = 0 To C2.Length - 1
    For j = 0 To C1.Length - 1
        If dict.ContainsKey(i + j) ' if dictionary contains row number
            dict(i + j) += C2(i) * C1(j) ' add value to existing row
        Else
            dict.Add(i + j, C2(i) * C1(j)) ' add new row
        End If
    Next
Next

For Each entry In dict
    Console.WriteLine("Total Row {0} = {1}", entry.Key, entry.Value)
Next

Windows窗体应用程序中的示例。在表单中添加一个DataGridView和一个Button。

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadInitialData()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Calculate()
    End Sub

    Sub LoadInitialData()
        DataGridView1.Rows.Clear()
        DataGridView1.Rows.Add(40, 1)
        DataGridView1.Rows.Add(30, 2)
        DataGridView1.Rows.Add(20, 3)
        DataGridView1.Rows.Add(10, Nothing)
    End Sub

    Sub Calculate()

        Dim dict = New Dictionary(Of Integer, Integer)

        For i = 0 To DataGridView1.Rows.Count - 1
            For j = 0 To DataGridView1.Rows.Count - 1
                ' check if both are numbers
                If IsNumeric(DataGridView1(0, i).Value) And IsNumeric(DataGridView1(1, j).Value) Then
                    Dim C1 = Convert.ToInt32(DataGridView1(0, i).Value) ' value of C1 from 1st column of outer loop
                    Dim C2 = Convert.ToInt32(DataGridView1(1, j).Value) ' value of C2 from 2nd column of inner loop

                    If dict.ContainsKey(i + j) Then ' check if dictionary has entry
                        dict(i + j) += C1 * C2 ' increment the value in dictionary
                    Else
                        dict.Add(i + j, C1 * C2) ' add new entry into dictionary
                    End If
                End If
            Next
        Next

        For Each entry In dict
            ' check if row in datagridview is lesser than dictionary entries
            If DataGridView1.Rows.Count < dict.Keys.Count Then
                DataGridView1.Rows.Add() ' add empty row
            End If
            DataGridView1(2, entry.Key).Value = entry.Value ' set value in 3rd column
        Next

    End Sub

End Class