Dear Sir,
I have an error "Conversion from type 'DBNull' to type 'String' is not valid " when creating an empty in the column Quantity, Price, Discount and I've used handle the error in DataGridView DataError event handler but still an error
In my code, I have marked which code is an error "'error in below line code"
can I resolve the error with 2 alternatives:
1. First, keep the datagridview column empty
2. The second automatically returns the previous first value
Please Guide me
Thanks
Public Class Form1
Public _myTable As New DataTable()
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))})
End Sub
Private Sub CalculateTotalqty()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim val As Double
'error in below line code
Double.TryParse(CType(item.Cells("Quantity").Value, String), val)
tot += val
Next item
lblQty.Text = tot.ToString("N2")
'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
Dim val3 As Double
Double.TryParse(CType(item.Cells("Quantity").Value, String), val)
'error in below line code
Double.TryParse(CType(item.Cells("Price").Value, String), val2)
'error in below line code
Double.TryParse(CType(item.Cells("Discount").Value, String), val3)
tot += (val * val2) - (val3)
Next item
lblValue.Text = tot.ToString("N")
'lblChange.Text = (cash - tot).ToString("N2")
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 grid_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())
Discount = Integer.Parse(DataGridView1.Rows(e.RowIndex).Cells("Discount").Value.ToString())
total = (quantity * price) - (Discount)
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