1 Attachment(s)
DataGridView quantity not adding up
I'm currently doing a Point of Sales System but I encountered a problem, the quantity and cost of item add up just fine in the DataGridView but not in the Subtotal, Tax, and Total. I don't know what I did wrong.
Here's my code:
Code:
Private Function Cost() 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 AddCost()
Dim vat, q As Double
vat = 3.9
If DataGridView1.Rows.Count > 0 Then
TxtSub.Text = FormatCurrency(Cost().ToString("0.00"))
TxtVAT.Text = FormatCurrency(((Cost() * vat) / 100).ToString("0.00"))
q = ((Cost() * vat) / 100)
TxtTotal.Text = FormatCurrency(q + Cost().ToString("0.00"))
End If
End Sub
Private Sub BtnTapsi_Click(sender As Object, e As EventArgs) Handles BtnTapsi.Click
Dim CostOfItem As Double = 60.0
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value = "Tapsilog" Then
row.Cells(1).Value = Double.Parse(row.Cells(1).Value) + 1
row.Cells(2).Value = Double.Parse(row.Cells(1).Value) * CostOfItem
Exit Sub
End If
Next
DataGridView1.Rows.Add("Tapsilog", "1", CostOfItem)
AddCost()
End Sub
And here's a sample image of what the problem looks like:
Attachment 183344
Re: DataGridView quantity not adding up
Can you post a screenshot of what it should look like?
Re: DataGridView quantity not adding up
I didn't spend the time to verify your problem but this caught my eye
Code:
Private Sub BtnTapsi_Click(sender As Object, e As EventArgs) Handles BtnTapsi.Click
Dim CostOfItem As Double = 60.0
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value = "Tapsilog" Then
row.Cells(1).Value = Double.Parse(row.Cells(1).Value) + 1
row.Cells(2).Value = Double.Parse(row.Cells(1).Value) * CostOfItem
Exit Sub
End If
Next
DataGridView1.Rows.Add("Tapsilog", "1", CostOfItem)
AddCost()
End Sub
Do you really want to call "Exit Sub"? When that happens this code will not be executed. If you want that code to execute then use "Exit For"
Code:
DataGridView1.Rows.Add("Tapsilog", "1", CostOfItem)
AddCost()
Re: DataGridView quantity not adding up
Not a screenshot but I want the same amount in the "Price" column to be displayed in the "Subtotal" textbox.
Quote:
Originally Posted by
.paul.
Can you post a screenshot of what it should look like?
Re: DataGridView quantity not adding up
This is imperfect, but closer to what you're looking for...
Code:
Private Function Cost() As Decimal
Dim sum As Decimal = 0
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim v As Decimal
Dim o As Object = DataGridView1.Rows(i).Cells(2).Value
If Decimal.TryParse(If(o IsNot Nothing, o.ToString, ""), v) Then
sum += v
End If
Next i
Return sum
End Function
Sub AddCost()
Dim vat, q, c As Decimal
vat = 3.9D
If DataGridView1.Rows.Count > 0 Then
c = Cost()
TxtSub.Text = c.ToString("c2")
q = CDec((c * vat) / 100)
TxtVAT.Text = q.ToString("c2")
TxtTotal.Text = (q + c).ToString("c2")
End If
End Sub
Private Sub BtnTapsi_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnTapsi.Click
Dim CostOfItem As Double = 60.0
For Each row As DataGridViewRow In DataGridView1.Rows
Dim o As Object = row.Cells(0).Value
If Not o Is Nothing Then
If o.ToString = "Tapsilog" Then
Dim o2 As Object = row.Cells(1).Value
If Not o2 Is Nothing Then
Dim v As Decimal
If Decimal.TryParse(o2.ToString, v) Then
row.Cells(1).Value = v + 1
row.Cells(2).Value = v * CostOfItem
End If
Exit For
End If
End If
End If
Next
DataGridView1.Rows.Add("Tapsilog", "1", CostOfItem)
AddCost()
End Sub
Re: DataGridView quantity not adding up
Code:
Private Sub BtnTapsi_Click(sender As Object, e As EventArgs) Handles BtnTapsi.Click
Dim CostOfItem As Double = 60.0
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value = "Tapsilog" Then
row.Cells(1).Value = Double.Parse(row.Cells(1).Value) + 1
row.Cells(2).Value = Double.Parse(row.Cells(1).Value) * CostOfItem
AddCost() ' Increasing the qty will now report the new total
Exit Sub
End If
Next
DataGridView1.Rows.Add("Tapsilog", "1", CostOfItem)
AddCost()
End Sub
1 Attachment(s)
Re: DataGridView quantity not adding up
Quote:
Originally Posted by
JL.19
I'm currently doing a Point of Sales System but I encountered a problem, the quantity and cost of item add up just fine in the DataGridView but not in the Subtotal, Tax, and Total. I don't know what I did wrong.
And here's a sample image of what the problem looks like:
Attachment 183344
I'm using the code inside DataGridView_CellLeave Event to handle Datagrid calculations.
My code could be confusing, but the point is, textbox values are updated each time selected cells are populated.
Code:
Private Sub DataGridView1_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
Dim total As Double = 0
Dim total_VAT As Double = 0
Dim total_price As Double = 0
Dim price_piece As Double = 0
Dim price As Double = 0
Dim VAT As Double = 0
Dim total_VAT_included As Double = 0
Dim discount As Double = 0
Dim VAT_1 As Double = 0
On Error Resume Next
If TextBox2.Text = "00" Then
VAT_1 = 0
Else
VAT_1 = (17 / 100)
End If
For i = 0 To DataGridView1.RowCount - 1
total_price += DataGridView1.Rows(i).Cells(7).Value()
total_VAT += DataGridView1.Rows(i).Cells(8).Value()
total += DataGridView1.Rows(i).Cells(9).Value()
discount = (DataGridView1.CurrentRow.Cells(6).Value / 100)
price_piece = DataGridView1.CurrentRow.Cells(4).Value * DataGridView1.CurrentRow.Cells(5).Value
price = (DataGridView1.CurrentRow.Cells(4).Value * DataGridView1.CurrentRow.Cells(5).Value) - (discount * price_piece)
VAT = price * VAT_1
total_VAT_included = price + VAT
DataGridView1.CurrentRow.Cells(7).Value = price
DataGridView1.CurrentRow.Cells(8).Value = VAT
DataGridView1.CurrentRow.Cells(9).Value = total_VAT_included
TextBox12.Text = total_price.ToString("n2")
TextBox13.Text = total_VAT.ToString("n2")
TextBox14.Text = total.ToString("n2")
Next
End Sub
Attachment 183362
Re: DataGridView quantity not adding up
@ivansmo… If you’re dealing with currency, I’d use Decimals instead of Doubles. Doubles are floating point numbers and nowhere near as accurate as Decimals. Also a Cell Value is type Object, and to be efficient you need to cast the Value as a numeric type.
Try turning Option Strict on in your project. Your code will light up like a Christmas tree😁👍🎄
Re: DataGridView quantity not adding up
@ivansmo… Also - On Error Resume Next… No-one uses that in VB.Net. It’s a holdover from classic VB. it’s far better to program defensively, anticipating errors and avoiding them, than skipping over them as you are.
Re: DataGridView quantity not adding up
Quote:
Originally Posted by
.paul.
@ivansmo… If you’re dealing with currency, I’d use Decimals instead of Doubles. Doubles are floating point numbers and nowhere near as accurate as Decimals. Also a Cell Value is type Object, and to be efficient you need to cast the Value as a numeric type.
Try turning Option Strict on in your project. Your code will light up like a Christmas tree😁👍🎄
Hey man, I saw your pages (games, wiki...) and I like them. I see you're an experienced programmer. ;)
I hope someday you'll give me some help :) Later, when a Christmass tree turns off...