Hello,
The following uses a class named Product, if using a DataTable or DataSet then adjustments are needed. The code finds rows with a Qty not null (Nothing)
To save and read back a List(Of ProductSave) use Json.net (the link shows serialize and deserialize in C# but is easy enough to convert to VB, if you get stuck I can help) via this package.
Product class
Public Class Product
Public Property Id() As Integer
Public Property Description() As String
Public Property Qty() As Integer?
Public Property UnitCost() As Decimal
Public ReadOnly Property Total() As Decimal
Get
If Qty.HasValue Then
Return CDec(Qty * UnitCost)
Else
Return 0
End If
End Get
End Property
Public Property Remarks() As String
End Class
Class to save rows
Public Class ProductSave
Public Property Id() As Integer
Public Property Qty() As Integer?
Public Property Remarks() As String
End Class
Mock up data
Public Class MockedData
Public Shared Function Products() As List(Of Product)
Dim productList = New List(Of Product)()
productList.Add(New Product() With {
.Id = 1,
.Description = "Item1",
.Qty = Nothing,
.UnitCost = 2.99D,
.Remarks = "Item 1 remarks"
})
productList.Add(New Product() With {
.Id = 2,
.Description = "Item2",
.Qty = 3,
.UnitCost = 10.99D,
.Remarks = "Item 2 remarks"
})
productList.Add(New Product() With {
.Id = 3,
.Description = "Item3",
.Qty = Nothing,
.UnitCost = 10.99D,
.Remarks = "Item 3 remarks"
})
productList.Add(New Product() With {
.Id = 4,
.Description = "Item3",
.Qty = 8,
.UnitCost = 1.99D,
.Remarks = "Item 4 remarks"
})
productList.Add(New Product() With {
.Id = 5,
.Description = "Item3",
.Qty = 8,
.UnitCost = 1.99D,
.Remarks = ""
})
Return productList
End Function
End Class
Form, one DataGridView, one Button
Public Class MainForm
Private _bindingSource As New BindingSource()
Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
DataGridView1.AllowUserToAddRows = False
_bindingSource.DataSource = MockedData.Products()
DataGridView1.DataSource = _bindingSource
End Sub
Private Sub GetRowsToSaveButton_Click(sender As Object, e As EventArgs) Handles GetRowsToSaveButton.Click
Dim results = DirectCast(_bindingSource.DataSource, List(Of Product)).
Where(Function(prod) prod.Qty.HasValue AndAlso Not String.IsNullOrWhiteSpace(prod.Remarks)).
ToList()
If results.Count <= 0 Then
Return
End If
Dim saved = New List(Of ProductSave)
For Each product In results
saved.Add(New ProductSave() With {.Id = product.Id, .Qty = product.Qty, .Remarks = product.Remarks})
Next
End Sub
Private Sub SaveTo(sender As List(Of ProductSave))
' Save information to say a .json file using json.net NuGet package
End Sub
Private Function GetSaved() As List(Of ProductSave)
' read from .json file created in SaveTo method above
Throw New NotImplementedException
End Function
End Class
Edit. I missed one part
Private Sub GetRowsToSaveButton_Click(sender As Object, e As EventArgs) Handles GetRowsToSaveButton.Click
Dim results = DirectCast(_bindingSource.DataSource, List(Of Product)).
Where(Function(prod) prod.Qty.HasValue AndAlso Not String.IsNullOrWhiteSpace(prod.Remarks)).
ToList()
If results.Count <= 0 Then
Return
End If
Dim saved = New List(Of ProductSave)
For Each product In results
saved.Add(New ProductSave() With {.Id = product.Id, .Qty = product.Qty, .Remarks = product.Remarks})
Next
SaveTo(saved)
End Sub
---