Hi kana250688,
Sorry for the late response.
Refer the below code which will suite as per your requirement.
Public Class Form1
Public _myTable As New DataTable()
Dim _code As String, _quantity As Integer, _price As Integer, _discount As Integer, _total As Integer
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
_myTable.Columns.AddRange({
New DataColumn("Code", GetType(String)),
New DataColumn("Quantity", GetType(Integer)),
New DataColumn("Price", GetType(Integer)),
New DataColumn("Discount", GetType(Integer)),
New DataColumn("Total", GetType(Integer))})
Me.CenterToScreen()
End Sub
Private Sub CalculateTotalqty()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim quantity As Double = 0
If Not IsDBNull(item.Cells("Quantity").Value) Then
quantity = CDbl(item.Cells("Quantity").Value)
End If
tot += quantity
Next item
lblQty.Text = tot.ToString("N2")
End Sub
Private Sub CalculateTotalValue()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim quantity As Double = 0
Dim price As Double = 0
Dim discount As Double = 0
If Not IsDBNull(item.Cells("Quantity").Value) Then
quantity = CInt(item.Cells("Quantity").Value)
End If
If Not IsDBNull(item.Cells("Price").Value) Then
price = CInt(item.Cells("Price").Value)
End If
If Not IsDBNull(item.Cells("Discount").Value) Then
discount = CInt(item.Cells("Discount").Value)
End If
tot += (quantity * price) - (discount)
Next item
lblValue.Text = tot.ToString("N")
End Sub
Private Sub BtnAddproduct_Click(sender As Object, e As EventArgs) Handles BtnAddproduct.Click
_myTable.Rows.Add("A", 2, 10000, 0, 20000)
'DataGridView1.AutoGenerateColumns = False
' Disable adding new row
DataGridView1.AllowUserToAddRows = False
' Create our medium between grid and collection
DataGridView1.DataSource = _myTable
CalculateTotalqty()
CalculateTotalValue()
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Dim quantity As Double = 0
Dim price As Double = 0
Dim discount As Double = 0
Dim total As Double = 0
If DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value IsNot Nothing Then
If Not IsDBNull(DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value) Then
quantity = CInt(DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value)
Else
MessageBox.Show("Quantity value is required.")
quantity = _quantity
End If
End If
DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value = quantity
If DataGridView1.Rows(e.RowIndex).Cells("Price").Value IsNot Nothing Then
If Not IsDBNull(DataGridView1.Rows(e.RowIndex).Cells("Price").Value) Then
price = CInt(DataGridView1.Rows(e.RowIndex).Cells("Price").Value)
Else
MessageBox.Show("Price value is required.")
price = _price
End If
End If
DataGridView1.Rows(e.RowIndex).Cells("Price").Value = price
If DataGridView1.Rows(e.RowIndex).Cells("Discount").Value IsNot Nothing Then
If Not IsDBNull(DataGridView1.Rows(e.RowIndex).Cells("Discount").Value) Then
discount = CInt(DataGridView1.Rows(e.RowIndex).Cells("Discount").Value)
Else
MessageBox.Show("Discount value is required.")
discount = _discount
End If
End If
DataGridView1.Rows(e.RowIndex).Cells("Discount").Value = discount
total = (quantity * price) - (discount)
DataGridView1.Rows(e.RowIndex).Cells("Total").Value = total
CalculateTotalqty()
CalculateTotalValue()
End Sub
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
If e.Context = DataGridViewDataErrorContexts.Commit Then
'MessageBox.Show(String.Format("Column {0} value is required.", e.ColumnIndex))
End If
End Sub
Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
_code = DataGridView1(0, e.RowIndex).Value.ToString()
_quantity = DataGridView1(1, e.RowIndex).Value.ToString()
_price = DataGridView1(2, e.RowIndex).Value.ToString()
_discount = DataGridView1(3, e.RowIndex).Value.ToString()
_total = DataGridView1(4, e.RowIndex).Value.ToString()
End Sub
End Class
Screenshot
