我正在为我的学院做一个项目,并且在尝试对DataGridView列进行求和时遇到了问题。我需要计算总金额。
这是我的代码:
Private Sub add()
Dim i As Integer = 0
dgvStock.Rows.Add()
dgvStock.Rows(dgvStock.RowCount - 1).Cells("srno").Value = txt_billno.Text
dgvStock.Rows(dgvStock.RowCount - 1).Cells("itemcode").Value = cmbItemCode.Text
dgvStock.Rows(dgvStock.RowCount - 1).Cells("itemname").Value = cmbItemName.Text
dgvStock.Rows(dgvStock.RowCount - 1).Cells("qty").Value = txtQty.Text
dgvStock.Rows(dgvStock.RowCount - 1).Cells("Rate").Value = txtRate.Text
dgvStock.Rows(dgvStock.RowCount - 1).Cells("Discount").Value = txtPerDisc.Text
dgvStock.Rows(dgvStock.RowCount - 1).Cells("PerTax").Value = txtPerVat.Text
txtAmount.Text = txtRate.Text * txtQty.Text
txtAmount.Text -= txtPerDisc.Text
' txtAmount.Text += txtPerDisc.Text / 100
dgvStock.Rows(dgvStock.RowCount - 1).Cells("Amount").Value = txtAmount.Text
txtGridAmt.Text = dgvStock.SelectedRows(0).Cells("Amount").Value
txtdiscRs.Text = dgvStock.SelectedRows(0).Cells("Discount").Value
txtVatRs.Text = dgvStock.SelectedRows(0).Cells("PerTax").Value
txtTotAmt.Text = txtGridAmt.Text
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Try
add()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'formclear()
End Sub
这是我的程序的快照,它将使我的问题更清楚 http://postimg.org/image/9ztydl5j1
答案 0 :(得分:1)
您添加或减去值的方法似乎不正确。由于.Text是String,而不是Integer。尝试更改此内容:
txtAmount.Text = txtRate.Text * txtQty.Text
txtAmount.Text -= txtPerDisc.Text
对此:
txtAmount.Text = CStr(CInt(txtRate.Text) * CInt(txtQty.Text))
txtAmount.Text = CStr(CInt(txtAmount.Text) - CInt(txtPerDisc.Text))
---编辑---
请改为尝试:
Dim NewRow As String() = {"", "", "", "", "", "", "", ""}
dgvStock.Rows.Add(NewRow)
---编辑2 ---
好:
Dim TotalAmount As Integer = 0
For x = 0 To dgvStock.Rows.Count - 1
TotalAmount += dgvStock.Rows(x).Cells("Amount").Value
Next
删除前:
TotalAmount -= dgvStock.Rows(dgvStock.SelectedRows(0)).Cells("Amount").Value