VB.NET DataGridview 和 Total

发布于 2025-01-17 19:48:42 字数 1266 浏览 3 评论 0原文

我正在尝试使用vb.net建立POS系统。我使用DataGridView显示项目,数量和数量。每当您单击项目所在的按钮时,它都会在DataGridView中进行更新。第三列是金额。总金额将显示为“总”。但是,每当我多次单击一个项目时,例如,每项50的金额,数量为3,因此总计150,但仅显示总金额为50。

我期望发生的是数量乘以数量和金额的总数(2x50 = 100),但发生的是(2x50 = 50),

这是我使用的代码: --------对于总计

Private Function Total_of_Products() As Double

    Dim Sum As Double = 0
    Dim i As Integer = 0

    For i = 0 To DataGridView1.Rows.Count - 1

        Sum = Sum + Convert.ToDouble(DataGridView1.Rows(i).Cells(2).Value)
    Next i
    Return Sum

End Function

Sub TotalProducts()

    If DataGridView1.Rows.Count > 0 Then
        total.Text = (Total_of_Products().ToString("0.00"))
    End If
End Sub

------------------------------------------------------

Private Sub toolbox_Click(sender As Object, e As EventArgs) Handles toolbox.Click

    Dim CostofItem As Double = 20
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells(0).Value = "toolbox" Then
            row.Cells(1).Value = Double.Parse(row.Cells(1).Value) + 1
            row.Cells(2).Value = CostofItem * Double.Parse(row.Cells(1).Value)
            Exit Sub

        End If

    Next
    DataGridView1.Rows.Add("toolbox", "1", CostofItem)
    TotalProducts()
End Sub

I am trying to make a POS system using VB.net. I used datagridview to show the item, quantity, and amount. Everytime you click the button where the item is located, it will update in the datagridview. The third column is the amount. the total amount will show in "TOTAL". But whenever i click an item more than once, example, the amount is 50 per item and the quantity is 3, therefore it should show in TOTAL 150, but it only shows the total amount is 50.

What i expect to happen is that the multiplied amount in qty and amount will show up in TOTAL section (2x50 = 100) but what happened is (2x50 = 50)

This are the codes I used:
---------For Total-----------

Private Function Total_of_Products() As Double

    Dim Sum As Double = 0
    Dim i As Integer = 0

    For i = 0 To DataGridView1.Rows.Count - 1

        Sum = Sum + Convert.ToDouble(DataGridView1.Rows(i).Cells(2).Value)
    Next i
    Return Sum

End Function

Sub TotalProducts()

    If DataGridView1.Rows.Count > 0 Then
        total.Text = (Total_of_Products().ToString("0.00"))
    End If
End Sub

----------For the Item------------

Private Sub toolbox_Click(sender As Object, e As EventArgs) Handles toolbox.Click

    Dim CostofItem As Double = 20
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells(0).Value = "toolbox" Then
            row.Cells(1).Value = Double.Parse(row.Cells(1).Value) + 1
            row.Cells(2).Value = CostofItem * Double.Parse(row.Cells(1).Value)
            Exit Sub

        End If

    Next
    DataGridView1.Rows.Add("toolbox", "1", CostofItem)
    TotalProducts()
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文