-
Dec 19th, 2021, 02:13 AM
#1
Thread Starter
Junior Member
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:
-
Dec 19th, 2021, 09:50 PM
#2
Re: DataGridView quantity not adding up
Can you post a screenshot of what it should look like?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 19th, 2021, 10:04 PM
#3
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()
-
Dec 20th, 2021, 01:59 AM
#4
Thread Starter
Junior Member
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.
Originally Posted by .paul.
Can you post a screenshot of what it should look like?
-
Dec 20th, 2021, 04:03 AM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 20th, 2021, 04:08 AM
#6
Addicted Member
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
-
Dec 20th, 2021, 04:27 PM
#7
Lively Member
Re: DataGridView quantity not adding up
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:
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
-
Dec 21st, 2021, 04:27 AM
#8
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😁👍🎄
Last edited by .paul.; Dec 21st, 2021 at 04:36 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 21st, 2021, 04:33 AM
#9
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.
Last edited by .paul.; Dec 21st, 2021 at 04:36 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 21st, 2021, 04:47 AM
#10
Lively Member
Re: DataGridView quantity not adding up
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...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|