我已经看过其他地方的解决方案,但是他们都使用设置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属性。有没有更好的方法呢?
使用NYSystemsAnalyst建议的解决方案:
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