gpt4 book ai didi

vb.net - 在两个datatable VB.net之间复制行

转载 作者:行者123 更新时间:2023-12-04 16:11:16 24 4
gpt4 key购买 nike

我有两个数据表,如何将目标行索引复制到同一索引中的另一个数据表,请检查以下代码。

            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

最佳答案

如果两个表具有相同的列,则可以使用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

关于vb.net - 在两个datatable VB.net之间复制行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15764027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com