Dear Sir
Why does a DataGridView row disappear when I first make empty in a particular column .
If I change the update in the "QTY" column to the value of 1 then I change it to the value of 0 after I empty it then the datagridview row is not lost but at the first time the form loads then I remove or make it blank in the qty column then the datagridview row becomes missing
Please guide me
Thanks
Result code in video
Imports System.ComponentModel
Imports System.Globalization
Imports System.Reflection
Public Class Form1
Private BindingSource1 As BindingSource = Nothing
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' To reduce the flickering...
DataGridView1.GetType().
GetProperty("DoubleBuffered",
BindingFlags.Instance Or BindingFlags.NonPublic).
SetValue(DataGridView1, True)
End Sub
Private Sub CalculateTotal()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim quantity As Double = 0
Dim price As Double = 0
If Not IsDBNull(item.Cells("Qty").Value) Then
Double.TryParse(CType(item.Cells("Qty").Value, String), quantity)
End If
If Not IsDBNull(item.Cells("PRICE").Value) Then
Double.TryParse(CType(item.Cells("PRICE").Value, String), price)
End If
tot += (quantity * price)
Next item
lblTotal.Text = tot.ToString("N")
End Sub
Protected Sub updateDataSource()
BindingSource1 = New BindingSource With {.DataSource = New BindingList(Of Products)}
DataGridView1.DataSource = BindingSource1
DataGridView1.AutoGenerateColumns = False
DataGridView1.AllowUserToAddRows = False
DataGridView1.Columns("PRICE").DefaultCellStyle.Format = "c2"
DataGridView1.Columns("PRICE").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US")
DataGridView1.Columns("TOTAL").DefaultCellStyle.Format = "c2"
DataGridView1.Columns("TOTAL").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US")
BindingSource1.AddNew()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
updateDataSource()
End Sub
Private Sub DataGridView1_CellEndEdit(
sender As Object,
e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Dim dgv = DirectCast(sender, DataGridView)
Dim PRICE As Double = Nothing, QTY As Double = Nothing, TOTAL As Double = Nothing
If DataGridView1.Rows(e.RowIndex).Cells("QTY").Value IsNot Nothing Then
If Not Double.TryParse(DataGridView1.Rows(e.RowIndex).Cells("QTY").Value.ToString(), QTY) Then
QTY = 0
End If
Else
QTY = 0
End If
DataGridView1.Rows(e.RowIndex).Cells("QTY").Value = QTY
If DataGridView1.Rows(e.RowIndex).Cells("PRICE").Value IsNot Nothing Then
If Not Double.TryParse(DataGridView1.Rows(e.RowIndex).Cells("PRICE").Value.ToString(), PRICE) Then
PRICE = 0
End If
Else
PRICE = 0
End If
DataGridView1.Rows(e.RowIndex).Cells("PRICE").Value = PRICE
TOTAL = (QTY * 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("value is required.", e.ColumnIndex))
End If
End Sub
End Class
Public Class Products
Public Property PRODUCTNAME() As String
Public Property QTY() As Double
Public Property PRICE() As Double
Public Property TOTAL() As Double
End Class