I am using ADO.NET.
I have 2 DataTables:
1) dt1
2) dt2
What I like to do is to copy just the header columns from dt1 and populate dt2 with it. Again I just need the column header.
What is the best approach to do this?
You need to Clone
the DataTable. Use DataTable.Clone
method.
DataTable dt = dt1.Clone();
If you just want to copy the column names and create columns with same name in the other DataTable you can do:
foreach (DataColumn dc in dt1.Columns)
{
dt2.Columns.Add(dc.ColumnName);
}
If you just need column names then you can get a List<string>
instead of a new DataTable. Like:
List<string> columnNames = dt1.Columns
.Cast<DataColumn>()
.Select(r => r.ColumnName)
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.