如何计算gridview行并将答案显示在文本框中

时间:2014-04-23 13:12:10

标签: sql vb.net

我正在尝试计算gridview行值并希望在文本框中显示。 我在gridview,itemid,itemname,orderedqty,price,totalprice中有5行。当我点击这里添加行时,我想计算总价格总额。想把它显示给文本框。

我在尝试这是代码,我认为它不起作用。请帮我找到解决方案。

Public Sub Calculate()
    Dim Countrow As Integer = 1
    Dim AmountTotal As Decimal = 0

    For Each row As DataGridView In dgvPurchaseOrder.Rows
        Countrow += 1
        AmountTotal = dgvPurchaseOrder.Rows.Count.ToString()
    Next
    'GridView1.FooterRow.Cells(1).Text = "Total = " & AmountTotal.ToString()
    txtGrossTotal.Text = AmountTotal.ToString()
End Sub

2 个答案:

答案 0 :(得分:0)

尝试这样

   Public Sub Calculate()
        Dim Countrow As Integer = 1
        Dim AmountTotal As Decimal = 0

        For Each row As DataGridView In dgvPurchaseOrder.Rows
            Countrow += 1
            AmountTotal += row.Cells(4).Value  // give correct price column index
        Next
               txtGrossTotal.Text = AmountTotal.ToString()
    End Sub

答案 1 :(得分:0)

Public Sub Calculate()
    Dim Countrow As Integer = 1
    Dim AmountTotal As Decimal = 0

    For Each row As DataGridViewRow In dgvPurchaseOrder.Rows
        Countrow += 1
        AmountTotal += row.Cells(4).Value
    Next
    txtGrossTotal.Text = AmountTotal.ToString()
End Sub

@equisde你是英雄,这是正确的代码。