Hi kana250688,
You need to handle the error in DataGridView DataError event handler.
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
Or make the class property nullable and check the condition for Price column as well.
Here is the updated code.
Imports System.ComponentModel
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
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 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
If DataGridView1.Rows(e.RowIndex).Cells("Price").Value IsNot Nothing Then
If Not Integer.TryParse(DataGridView1.Rows(e.RowIndex).Cells("Price").Value.ToString(), price) Then
price = 0
End If
Else
price = 0
End If
total = (quantity * price)
DataGridView1.Rows(e.RowIndex).Cells("Total").Value = total
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
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