DataGridView VB.net中的计算
我正在VB.net中构建一个销售点系统,并使用DataGridView显示我的数据。当我将“价格”列相加时,将显示总数,但如果我按下“总数”按钮,则数字将加倍,并且每次按下时,数字将继续加倍。此外,我想知道如何包括一个按钮,我可以输入收到的金额和需要给予的变化?我将感谢任何帮助,因为我是一个全新的VBDataGridView VB.net中的计算,vb.net,datagridview,Vb.net,Datagridview,我正在VB.net中构建一个销售点系统,并使用DataGridView显示我的数据。当我将“价格”列相加时,将显示总数,但如果我按下“总数”按钮,则数字将加倍,并且每次按下时,数字将继续加倍。此外,我想知道如何包括一个按钮,我可以输入收到的金额和需要给予的变化?我将感谢任何帮助,因为我是一个全新的VB Private Sub btnSandAndSoup_Click(sender As Object, e As EventArgs)
Private Sub btnSandAndSoup_Click(sender As Object, e As EventArgs)
Handles btnSandAndSoup.Click
DataGridView1.ColumnCount = 4
DataGridView1.Columns(0).Name = "Bar Code"
DataGridView1.Columns(1).Name = "Type"
DataGridView1.Columns(2).Name = "Name"
DataGridView1.Columns(3).Name = "Price"
Dim row As String() = New String() {"0037853", "Food", "Sandwhich Soup",
"10"}
DataGridView1.Rows.Add(row)
End Sub
Private Sub btnChickenWings_Click(sender As Object, e As EventArgs
Handles btnChickenWings.Click
DataGridView1.ColumnCount = 4
DataGridView1.Columns(0).Name = "Bar Code"
DataGridView1.Columns(1).Name = "Type"
DataGridView1.Columns(2).Name = "Name"
DataGridView1.Columns(3).Name = "Price"
Dim row As String() = New String() {"0037834", "Food", "Chicken Wings", "12"}
DataGridView1.Rows.Add(row)
End Sub
Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click
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(3).Value
Next
DataGridView1.Rows(max).Cells(3).Value += tot
DataGridView1.Rows(max).Cells(2).Value = total
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
有两种行类型-
DataRow
和AlternatingDataRow
。仅当行是这两种类型之一时,才循环和总计
将总控件放在页脚模板中。在循环期间,它们不会被添加到总和中。您正在迭代所有行:
在DataGridView1中将每行作为DataGridViewRow。行
和总计行是其中之一,因此其值也会被添加(并重新添加)