【问题标题】:How to calculate total amount in every row in DataGridView using SQL Database如何使用 SQL 数据库计算 DataGridView 中每一行的总金额
【发布时间】:2021-06-09 14:12:56
【问题描述】:

示例项目输出

我正在使用 vb.net 和 SQL 数据库

谁能教我如何使用按钮来计算 Datagridview 中 Row 的总量。 我不知道我该怎么做。

例如

    ProductCost              ProductQuantity               TotalAmount

       32                        20                          640

       32                        32                          1024

Label3 = TotalAmount

应该是这样的

    ProductCost              ProductQuantity               TotalAmount

       32                        20                          640

       32                        32                          1024

标签 3 = 640 标签3 = 1024

而且我可以不断添加新项目,它会自动计算。

【问题讨论】:

  • 一个好的开始是考虑实际问题所在。您要么使用 SQL 对数据库中的数据求和,要么使用 VB 对应用程序中的数据求和。不可能两者兼而有之。如果你有一个DataTable 包含数据,那么它来自哪里是无关紧要的。你在网格中显示它是无关紧要的,因为它发生了。 DataTable 有一个 Compute 方法。你应该使用它。

标签: sql vb.net


【解决方案1】:
  Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
        Try
            For i = 0 To DataGridView1.RowCount - 1
                DataGridView1.Item("TotalAmount", i).Value = DataGridView1.Item("ProductCost", i).Value * DataGridView1.Item("ProductQuantity", i).Value
            Next

        Catch ex As Exception

        End Try
    End Sub

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多