C# 将DataGridViewRow转换为DataRow

C# 将DataGridViewRow转换为DataRow,c#,vb.net,datagridview,C#,Vb.net,Datagridview,我在其他地方见过解决方案,但它们都使用了将DataRowView设置为与我的DataGridViewRow.DataBoundItem相等的想法,然后使用DataRowView.Row属性(在下面的代码中) 这是我的密码: Dim tblTemp As New DataTable() Dim row As Windows.Forms.DataGridViewRow Dim rowView As DataRowView For Each row In grdLabels.SelectedRows

我在其他地方见过解决方案,但它们都使用了将DataRowView设置为与我的DataGridViewRow.DataBoundItem相等的想法,然后使用DataRowView.Row属性(在下面的代码中)

这是我的密码:

Dim tblTemp As New DataTable()
Dim row As Windows.Forms.DataGridViewRow
Dim rowView As DataRowView

For Each row In grdLabels.SelectedRows
    If (row.Cells("Status").Value = Status.RECIEVED) Then
        rowView = DirectCast(row.DataBoundItem, DataRowView)
        tblTemp.Rows.Add(rowView.Row)
    End If
Next
问题是,如果您这样做是为了将数据从一个表复制到另一个表,则会出现错误“此行已属于另一个表”。有没有办法复制这一行,而不是引用它?我不希望只需循环遍历每个单元格,复制其Value属性。有更好的方法吗

使用NYSystems分析师建议的解决方案:

Dim tblTemp As New DataTable()
Dim row As Windows.Forms.DataGridViewRow
Dim rowView As DataRowView

If TypeOf grdLabels.DataSource Is DataTable Then
    tblTemp = CType(grdLabels.DataSource, DataTable).Clone()
End If

For Each row In grdLabels.SelectedRows
    If (row.Cells("Status").Value = Status.RECIEVED) Then
        rowView = DirectCast(row.DataBoundItem, DataRowView)
        tblTemp.ImportRow(rowView.Row)
    End If
Next
请参阅此链接:

您需要使用ImportRow()方法