我如何总计来自 datagridview vb.net 的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23858800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:31:28  来源:igfitidea点击:

how do i total the sum from datagridview vb.net

vb.netvb.net-2010

提问by LordWelhimNoob

-how do i get the sum of totalPrice in my datagridview from mysql

-如何从 mysql 获取 datagridview 中的 totalPrice 的总和

ProductName          Qty.   Price    totalPrice
2 Pcs. Chickenjoy     5      59         295
2 Pcs. Chickenjoy     1      69          69
2 Pcs. Chickenjoy     1      69          59

                                  TOTAL??

-the sum should be 423 the problem is, it will double the sum

-总和应该是 423 问题是,它会加倍

-here's my code:

- 这是我的代码:

Try
    'declaring variable as integer to store the value of the total rows in the datagridview

    Dim max As Integer = DataGridView1.Rows.Count - 1
    Dim total As String = "Total ----------->"
    'getting the values of a specific rows


    For Each row As DataGridViewRow In DataGridView1.Rows
        'formula for adding the values in the rows
        DataGridView1.Rows(max).Cells(4).Value += row.Cells(4).Value
        DataGridView1.Rows(max).Cells(3).Value = total
    Next
Catch ex As Exception
    MsgBox(ex.Message)
End Try

Screenshots http://goo.gl/Ufj53b

截图 http://goo.gl/Ufj53b

回答by tinstaafl

Your main problem appears to be using a For Each loop. When it reaches the last row it's adding the accumulated total to itself. A For loop to only the second last row should work:

您的主要问题似乎是使用 For Each 循环。当它到达最后一行时,它会将累计总数加到自身上。只到倒数第二行的 For 循环应该可以工作:

Try
    'declaring variable as integer to store the value of the total rows in the datagridview

    Dim max As Integer = DataGridView1.Rows.Count - 1
    'getting the values of a specific rows
    DataGridView1.Rows(max).Cells(3).Value = "Total ----------->"

    For I = 0 To DataGridView1.Rows.Count - 2
        'formula for adding the values in the rows
        DataGridView1.Rows(max).Cells(4).Value += DataGridView1.Rows(I).Cells(4).Value
    Next
Catch ex As Exception
    MsgBox(ex.Message)
End Try

回答by The Blue Dog

Try this:

尝试这个:

Dim tot As Integer
For Each row As DataGridViewRow In DataGridView1.Rows
    'formula for adding the values in the rows
    tot += row.Cells(4).Value
Next
DataGridView1.Rows(max).Cells(3).Value = total
DataGridView1.Rows(max).Cells(4).Value = tot

回答by ViSTA

your loop has an error. because it will count the sum of 295,69 and 59 then that added to the sum again. thats why it will double the sum. try this

你的循环有错误。因为它将计算 295,69 和 59 的总和,然后再次添加到总和中。这就是为什么它会使总和增加一倍。尝试这个

Try
    'declaring variable as integer to store the value of the total rows in the datagridview

    Dim max As Integer = DataGridView1.Rows.Count - 1
    Dim total As String = "Total ----------->"
    Dim tot as integer =0 
    'getting the values of a specific rows


    For Each row As DataGridViewRow In DataGridView1.Rows
        'formula for adding the values in the rows
        tot += row.Cells(4).Value
    Next
    DataGridView1.Rows(max).Cells(4).Value += tot
        DataGridView1.Rows(max).Cells(3).Value = total
Catch ex As Exception
    MsgBox(ex.Message)
End Try