Mohammed Serrar Mohammed Serrar - 6 months ago 13
Vb.net Question

Insert headers of a datatable VB.NET

I need your help for my code vb
In fact, i created an new datatable and i want to copy the headers from another datatable
here is my code

Dim name(de.Tables(0).Columns.Count) As String
Dim p As Integer = 0
For Each column As DataColumn In de.Tables(0).Columns
name(p) = column.ColumnName
p += 1
Next
Dim m As Integer = 0
For m = 0 To de.Tables(0).Columns.Count - 1
dt.Columns(m).ColumnName = name(p)
Next

Answer

If you only want to "copy" the schema of a DataTable(so the columns and constraints) without it's content(DataRows) you can use DataTable.Clone:

Dim clonedTable As DataTable = originalTable.Clone()

If you also want to copy the DataRows you have to use DataTable.Copy.

Comments