更新时间:2023-12-02 18:28:52
我建议阅读本文: Linq到VB.Net中的datagridview [ ^ ]。作者建议使用扩展方法,但我认为可以以更简单的方式实现:
I'd suggest to read this: Linq to datagridview in VB.Net[^]. Author proposes to use extension method, but i think it's possible to achieve in a simpler manner:
'get selected rows from dgv1 as a list of DataGridViewRow
Dim selectedRows As List(Of DataGridViewRow) = (From row In dgvShow.Rows.Cast(Of DataGridViewRow)() _
Where row.Cells("checkBoxColumn").Value _
Select New DataGridViewRow With
{
're-write columns, skip checkBoxColumn
}).ToList()
'add new rows to DataGridView2
DataGridView2.DataSource = selectedRows.Clone()
注意:未经测试!
如果它适用于您,请告诉我。
Note: Not tested!
Please, let me know if it works for you or not.
这是我复制的代码选择从Datagridview1到Datagridview2的行
Here is my code to copy the selected Rows from Datagridview1 to Datagridview2
Try
Dim selectedRows As List(Of DataGridViewRow) = (From row In dgvShow.Rows.Cast(Of DataGridViewRow)() Where Convert.ToBoolean(row.Cells("checkBoxColumn").Value) = True).ToList()
If selectedRows.Count > 0 Then
If DataGridView2.Columns.Count < 0 Then
For Each c As DataGridViewColumn In dgvShow.Columns
DataGridView2.Columns.Add(TryCast(c.Clone(), DataGridViewColumn))
Next
End If
For Each r As DataGridViewRow In selectedRows
Dim index As Integer = DataGridView2.Rows.Add(TryCast(r.Clone(), DataGridViewRow))
For Each o As DataGridViewCell In r.Cells
DataGridView2.Rows(index).Cells(o.ColumnIndex).Value = o.Value
r.Cells(0).Value = False
Next
Next
End If
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End Try