Copying datatables and datarows

G_Waddell 0 Tallied Votes 1K Views Share

Hi
Ever had the situation where you need to make a copy of a Datatable? Or where you wish to copy a datarow from one datatable to another?

Well here is how by using Clone, Copy and the ImportRow DataTable Methods.

Function Copy_A_Datatable (byref src as datatable) as datatable
'Copies a datatable including Schema, constraints and rows over to another one
dim dt as DataTable
dt = src.Copy
Return dt
End Function

Function Clone_A_Table(byref src as datatable) as datatable
'create new Blank table based on source with same schema etc just no data
dim dt as Datatable
dt= src.clone
return dt 
end function

Sub Import_A_DataRow(byref DR as dataRow, Target as DataTable)
'Imports a datarow of same schema from another table
Target.ImportRow(DR)
end sub