2009-09-17 337 views
4

ここでは、DataGridViewRow.DataBoundItemと等しいDataRowViewを設定し、次にDataRowView.Rowプロパティを使用するアイディアを使用しています(下のコードを参照)。ここでDataGridViewRowをDataRowに変換する

は私のコードです:

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 

答えて

関連する問題