将选定的行从DataGridView复制到另一行,包括“图像列" [英] Copy selected rows from a DataGridView to another, including Image Column
问题描述
我目前正在尝试将选定的行从一个DataGridView复制到另一个.
我正在尝试捕获CheckBox的值,如果选中它,那么整行将被复制到另一个DataGridView.
I'm currently trying to copy selected rows from one DataGridView to another.
I'm trying to capture the value of the CheckBox, where if it's checked, then the entire row will be copied to another DataGridView.
例如,先添加到购物车,然后再查看购物车.我提到了以下帖子:
以不同的形式将选定的数据网格复制到新的datagridview
For example, like add to cart then review cart. I've referred to the following post:
Copy selected datagridrow to new datagridview on different form
但是似乎没有帮助.
我已经尝试过使用下面的For
循环,但是我不确定如何执行此操作.
However it doesn't seem to help.
I've tried using a For
loop like the one below, but I'm not entirely sure how to go about this.
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
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
我在这里做的没有用,我也不知道该怎么办.
任何帮助将不胜感激,谢谢.
What I'm doing here isn't working and I have no idea how to go about this.
Any help would be appreciated, thank you, kindly.
每当我运行此代码时,都会出现错误:
Whenever I run this code, I get the error:
System.NullReferenceException:对象引用未设置为实例 一个对象
System.NullReferenceException: Object reference not set to an instance of an object
我不确定该如何解决.
这是DataGridView的样子:
This is what the DataGridView looks like:
推荐答案
此问题与上一个问题严格相关:
使用JSON对象作为数据源在DataGridView列中显示图像
This question is strictly related to the previous one:
Display images in a DataGridView column using JSON objects as DataSource
您正在使用 RootObject
的子类( Result
)来填充第一个DataGridView.
You're using a sub-class (Result
) of the RootObject
to fill the first DataGridView.
修改 Result
类,如下所示:
Modify the Result
class as follows:
- 添加一个新属性,
Selected As Boolean
,该属性以<JsonIgnore>
属性装饰. - 在此处添加一个名为
SelectionResult
的新子类,并选择第二个DataGridView中需要的Result
类的属性.显示选定的产品. - 向
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
- 在表单中添加两个
List(Of Class)
作为字段.在上一个问题中,主类称为ProductsQuery
,因此,我将重用已在此处定义的名称: - Add two
List(Of Class)
as Fields in the Form. The main class, in the previous question, was calledProductsQuery
, 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)()
-
在填充第一个DataGridView的方法中,初始化
CurrentProducts
列表:In the method that fills the first DataGridView, initialize the
CurrentProducts
List:CurrentProducts = New List(Of ProductsQuery.Result)()
-
将JSON反序列化后,用JSON结果填充列表:
After the JSON has beed deserialized, fill the List with the JSON results:
CurrentProducts.AddRange(JsonPost.uk.ghs.Products.Results)
在将所选产品添加到第二个DataGridView的Button的事件处理程序中,插入以下代码:
In the event handler of the Button that adds the selected products to the second DataGridView, insert this code:
修改:
SelectedProducts
列表保留在第一个DataGridView中选择的项目:仅将CurrentProducts
列表中尚未存在的项目添加到选择中.Edit:
TheSelectedProducts
list preserves the items selected in the first DataGridView: only the items that are not already in theCurrentProducts
list are added to the selection.btnRemoveSelection
按钮从SelectedProducts
列表中删除第二个DataGridView中的选定项. DataGridView行的选择有些麻烦,因此可能要添加一个CheckBox列以简化选择要删除的项目的过程.The
btnRemoveSelection
Button removes the selected items in the second DataGridView from theSelectedProducts
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
这将用第一个DataGridView的选定元素填充
List(Of SelectedProducs)
,并将第二个DataGridView的DataSource设置为此列表.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.请注意,DataGridView的第一列设置为
Visible = False
,因为该列对应于所选元素的ID
属性Note that the first Column of the DataGridView is set to
Visible = False
, because that Column corresponds to theID
property of the element selectedResult
类的GetSelectionResult()
返回在SelectionResult
类中定义的属性值.您当然可以重新定义此类,以包含您认为合适的任何属性.The
GetSelectionResult()
of theResult
class returns the properties values that have been defined in theSelectionResult
class. You can of course re-define this class to contain whatever properties you see fit.这是这些修改的结果:
这篇关于将选定的行从DataGridView复制到另一行,包括“图像列"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!