This question is strictly related to the previous one:
Display images in a DataGridView column using JSON objects as DataSource
You're using a sub-class (Result
) of the RootObject
to fill the first DataGridView.
Modify the Result
class as follows:
- Add a new property,
Selected As Boolean
, decorated with a <JsonIgnore>
attribute.
- Add a new sub-class, called
SelectionResult
here, a selection of properties of the Result
class that you think are needed in the second DataGridView to show the selected products.
- Add a copy method to the
Result
class which returns a sub-section of itself as a SelectionResult
object.
Public Class Result
<JsonIgnore>
Public Property Selected As Boolean
'(...)
Public Function GetSelectionResult() As SelectionResult
Return New SelectionResult With {
.ID = Me.id,
.Image = Me.Image,
.Name = Me.Name,
.ProductDescription = Me.ProductDescription,
.Department = Me.Department,
.Price = Me.Price,
.Unitprice = Me.Unitprice
}
End Function
End Class
Public Class SelectionResult
Public Property ID As Integer
Public Property Image As Bitmap
Public Property Name As String
Public Property ProductDescription As String
Public Property Department As String
Public Property Price As Decimal
Public Property Unitprice As Decimal
End Class
- Add two
List(Of Class)
as Fields in the Form. The main class, in the previous question, was called ProductsQuery
, so I'm re-using the names already defined there:
Private CurrentProducts As List(Of ProductsQuery.Result) = New List(Of ProductsQuery.Result)()
Private SelectedProducts As List(Of ProductsQuery.SelectionResult) = New List(Of ProductsQuery.SelectionResult)()
In the method that fills the first DataGridView, initialize the CurrentProducts
List:
CurrentProducts = New List(Of ProductsQuery.Result)()
After the JSON has beed deserialized, fill the List with the JSON results:
CurrentProducts.AddRange(JsonPost.uk.ghs.Products.Results)
In the event handler of the Button that adds the selected products to the second DataGridView, insert this code:
Edit:
The SelectedProducts
list preserves the items selected in the first DataGridView: only the items that are not already in the CurrentProducts
list are added to the selection.
The btnRemoveSelection
Button removes the selected items in the second DataGridView from the SelectedProducts
list. The DataGridView Row selection is somewhat cumbersome, so might want to add a CheckBox Column to ease the selection of the items to remove.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
SelectedProducts.AddRange(CurrentProducts.
Where(Function(p) p.Selected = True AndAlso
(Not SelectedProducts.Any(Function(sp) sp.ID = p.id))).
Select(Function(p) p.GetSelectionResult()).ToArray())
ResetCart()
End Sub
Private Sub btnRemoveSelection_Click(sender As Object, e As EventArgs) Handles btnRemoveSelection.Click
If DataGridView2.SelectedRows.Count = 0 Then Return
Dim itemsRemoved As Boolean = False
Dim selectedItems() As Integer = DataGridView2.SelectedRows.
OfType(Of DataGridViewRow)().
Select(Function(r) CInt(r.Cells("ID").Value)).ToArray()
For Each ID As Integer In selectedItems
Dim currentIndex As Integer = SelectedProducts.FindIndex(Function(p) p.ID = ID)
If currentIndex >= 0 Then
SelectedProducts.RemoveAt(currentIndex)
itemsRemoved = True
End If
Next
If itemsRemoved Then
ResetCart()
End If
End Sub
Private Sub ResetCart()
DataGridView2.DataSource = Nothing
DataGridView2.DataSource = SelectedProducts
DataGridView2.Columns(0).Visible = False
DataGridView2.AutoResizeRows()
End Sub
This fills the List(Of SelectedProducs)
with the selected elements of the first DataGridView and sets the DataSource of the second DataGridView to this List.
Note that the first Column of the DataGridView is set to Visible = False
, because that Column corresponds to the ID
property of the element selected
The GetSelectionResult()
of the Result
class returns the properties values that have been defined in the SelectionResult
class. You can of course re-define this class to contain whatever properties you see fit.
This is the result of these modifications:
