在两个数据表 VB.net 之间复制行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15764027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy rows between two datatable VB.net
提问by KaZzA
I have two datatable how i could copy targeted rows index to another datatable in the same index, Please check below code.
我有两个数据表,如何将目标行索引复制到同一索引中的另一个数据表,请检查下面的代码。
Dim datatable1 As DataTable = GetEmployeeSummary()
Dim datatable2 As DataTable = GetEmployees()
For i As Integer = 0 To datatable1.Rows.Count - 1 'Datatable1.rows.count = datatable2.rows.count
Select Case i
Case 1, 5, 6, 19, 24
datatable2.Rows(i) = datatable2.Rows(i) 'how i could copy targeted rows index to another datatable in the same index
End Select
Next
回答by Tim Schmelter
You can use the DataRow.ItemArrayif both tables have the same columns:
DataRow.ItemArray如果两个表具有相同的列,您可以使用:
For i As Int32 = 0 To datatable1.Rows.Count - 1
Select Case i
Case 1, 5, 6, 19, 24
If datatable2.Rows.Count - 1 >= i Then
datatable2.Rows(i).ItemArray = datatable1(i).ItemArray
Else
Dim row = datatable2.Rows.Add()
row.ItemArray = datatable1(i).ItemArray
End If
End Select
Next
回答by Menuka Ishan
I recommend of using ImportRow. It will copy the whole row into your DataTable. So Your code would be like below.
我建议使用ImportRow. 它会将整行复制到您的数据表中。所以你的代码将如下所示。
Dim datatable1 As DataTable = GetEmployeeSummary()
Dim datatable2 As DataTable = GetEmployees()
For i As Integer = 0 To datatable1.Rows.Count - 1
Select Case i
Case 1, 5, 6, 19, 24
datatable2.ImportRow(datatable2.Rows(i))
End Select
Next

