Dear Sir,
I'm Trying use event CellEndEdit datagridview but the CellEndEdit datagridview event not update in the "Total" column when add Quantity plus .
Please Guide me.
Public Class Form1
Private list As BindingList(Of Product) = Nothing
Private bindingSource As BindingSource = Nothing
Public Sub New()
InitializeComponent()
npQuantity.Maximum = Decimal.MaxValue
npQuantity.Minimum = 1
End Sub
' Retrieve product from selected row.
Private Function GetProduct() As Product
Return If(DataGridView1.SelectedCells.Count = 0, Nothing, TryCast(DataGridView1.SelectedCells(0).OwningRow.DataBoundItem, Product))
End Function
Private Sub CalculateTotalqty()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim val As Double
Double.TryParse(CType(item.Cells("Quantity").Value, String), val)
tot += val
Next item
lblQty.Text = tot.ToString("N")
'lblChange.Text = (cash - tot).ToString("N2")
End Sub
Private Sub CalculateTotalValue()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim val As Double
Dim val2 As Double
Double.TryParse(CType(item.Cells("Quantity").Value, String), val)
Double.TryParse(CType(item.Cells("Price").Value, String), val2)
tot += (val * val2)
Next item
lblValue.Text = tot.ToString("N")
'lblChange.Text = (cash - tot).ToString("N2")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnPlusQty_Click(sender As Object, e As EventArgs) Handles btnPlusQty.Click
If DataGridView1.Rows.Count > 0 Then
' Change quantity here.
Dim product = GetProduct()
If product IsNot Nothing Then
' Update product's quantity.
product.Quantity += npQuantity.Value
' Do not forget to refresh grid.
DataGridView1.Refresh()
End If
End If
CalculateTotalqty()
CalculateTotalValue()
End Sub
Private Sub btnMinusQty_Click(sender As Object, e As EventArgs) Handles btnMinusQty.Click
If DataGridView1.Rows.Count > 0 Then
' Change quantity here.
Dim product = GetProduct()
If product IsNot Nothing And product.Quantity > 1 Then
'If npQuantity.Value > 0 Then
' Update product's quantity.
product.Quantity = product.Quantity - 1
' Do not forget to refresh grid.
DataGridView1.Refresh()
Else
MessageBox.Show("Quantity column cannot be minus")
End If
End If
CalculateTotalqty()
CalculateTotalValue()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnAddproduct.Click
Dim List As New List(Of Product)()
List.Add(New Product() With {
.Code = "A",
.Quantity = 2,
.Price = 10000,
.Total = 20000
})
List.Add(New Product() With {
.Code = "B",
.Quantity = 2,
.Price = 20000,
.Total = 40000
})
Dim bindingList As New BindingList(Of Product)(List)
DataGridView1.AutoGenerateColumns = False
' Disable adding new row
DataGridView1.AllowUserToAddRows = False
' Create our medium between grid and collection
bindingSource = New BindingSource With {.DataSource = List}
DataGridView1.DataSource = bindingSource
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
CalculateTotalqty()
CalculateTotalValue()
Dim price As Integer = Nothing, quantity As Integer = Nothing, Discount As Integer = Nothing, total As Integer = Nothing
If DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value IsNot Nothing Then
If Not Integer.TryParse(DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value.ToString(), quantity) Then
quantity = 0
End If
Else
quantity = 0
End If
price = Integer.Parse(DataGridView1.Rows(e.RowIndex).Cells("Price").Value.ToString())
total = (quantity * price)
DataGridView1.Rows(e.RowIndex).Cells("Total").Value = total
End Sub
End Class
Friend Class Product
Public Property Code As String
Public Property Quantity As Decimal
Public Property Price As Integer
Public Property Total As Integer
End Class