将选定的复选框行从DGV复制到DGV2(包括图像列)

Sch*_*ler 2 vb.net datagridview image winforms

我目前正在尝试将选定的行从一个DataGridView复制到另一个.
我正在尝试捕获CheckBox的值,如果选中它,那么整行将被复制到另一个DataGridView.
例如,像添加到购物车然后审查购物车.我已经提到了以下帖子:
将选定的datagridrow复制到不同表单上的新datagridview

但它似乎没有帮助.
我尝试使用For类似下面的循环,但我不完全确定如何去做.

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Dim dt As New DataTable()
    AppendColumnsToDGV2()
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells("SelectColumn").Value = True Then
            Dim NewRow As DataRow
            For i As Integer = 0 To row.Cells.Count - 1
                NewRow(i) = row.Cells(i).Value
                DataGridView2.Rows.Add(NewRow)
            Next
        End If
    Next
Run Code Online (Sandbox Code Playgroud)

AppendColumnsToDGV2:

  Private Sub AppendColumnsToDGV2()
      Dim dt As New DataTable
      'dt.Columns.Add(CreateDGVCheckBoxCol())
      'dt.Columns.Add(CreateImageColumn())
      dt.Columns.Add(DataGridView1.Columns(3).HeaderText)
      dt.Columns.Add(DataGridView1.Columns(4).HeaderText)
      dt.Columns.Add(DataGridView1.Columns(5).HeaderText)
      dt.Columns.Add(DataGridView1.Columns(6).HeaderText)
      DataGridView2.DataSource = dt
End Sub
Run Code Online (Sandbox Code Playgroud)

我在这里做的不起作用,我不知道该怎么做.
任何帮助将不胜感激,谢谢,亲切.

每当我运行此代码时,我都会收到错误:

System.NullReferenceException:未将对象引用设置为对象的实例

我不确定如何解决它.

这就是DataGridView的样子:

WhatDGVLooksLike

Jim*_*imi 5

此问题与前一个问题严格相关:
使用JSON对象作为DataSource在DataGridView列中显示图像

您正在使用填充第一个DataGridView 的子类(Result)RootObject.

Result按如下方式修改类:

  • 添加一个新属性,Selected As Boolean<JsonIgnore>属性修饰.
  • 添加一个新的子类,SelectionResult在此处调用Result,您认为在第二个DataGridView中需要的类的属性选择,以显示所选的产品.
  • Result类中添加一个复制方法,该方法将自身的子部分作为SelectionResult对象返回.

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
Run Code Online (Sandbox Code Playgroud)
  • List(Of Class)在表单中添加两个作为字段.在上一个问题中,主类被调用ProductsQuery,所以我重新使用已在那里定义的名称:

Private CurrentProducts As List(Of ProductsQuery.Result) = New List(Of ProductsQuery.Result)()
Private SelectedProducts As List(Of ProductsQuery.SelectionResult) = New List(Of ProductsQuery.SelectionResult)()
Run Code Online (Sandbox Code Playgroud)

在Button的事件处理程序中,将所选产品添加到第二个DataGridView,请插入以下代码:

编辑:
SelectedProducts列表保存在第一DataGridView中选择的项目:只有在没有项目CurrentProducts列表中添加到选择.

btnRemoveSelection按钮删除从第二DataGridView中选定的项目SelectedProducts清单.DataGridView行选择有点麻烦,因此可能需要添加CheckBox列以便于选择要删除的项目.

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
Run Code Online (Sandbox Code Playgroud)

这将填充List(Of SelectedProducs)第一个DataGridView的选定元素,并将第二个DataGridView的DataSource设置为此List.

请注意,DataGridView的第一列设置为Visible = False,因为该列对应于ID所选元素的属性

GetSelectionResult()该的Result类返回已在已定义的属性值SelectionResult类.您当然可以重新定义此类以包含您认为合适的任何属性.


这是这些修改的结果:

DataGridView JSON结果