VB.NET - Copy rows between data tables

preview_player
Показать описание
Here is the code you can use to copy rows from one data table to another.

VB.NET - Copy rows between data tables
-----------------------------------------------
Dim tableOld As New DataTable

'Create 2 columns
tableOld.Columns.Add("ProductID", GetType(Integer))
tableOld.Columns.Add("ProductName", GetType(String))

'Add 2 rows
tableOld.Rows.Add(111, "Apple")
tableOld.Rows.Add(112, "Orange")

Dim tableNew As New DataTable

'Copy table structure of tableOld to tableNew
tableNew = tableOld.Clone()

Dim rowNumber As Integer = 1 'Change the row number as needed
tableNew.ImportRow(tableOld.Rows(rowNumber)) 'Copies a certain row of tableOld into tableNew

'You can only copy an exact row into another table using this approach if both of them have the same table structure.
'That is why we used the Clone() method to copy the table structure.

MessageBox.Show(tableNew.Rows(0).Item("ProductID") & ": " & tableNew.Rows(0).Item("ProductName"))
Рекомендации по теме
join shbcf.ru