Copy one datatable values to another datatable dynamically
Hi,How to get columns and row values dynamically and add into new datatable dyanamically.
ex : 1st datatable is filled with 3 columns and 2 row values. i need to get columns name and row values dynamically and assign it to new dt.
My code :
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
DataTable dt = new DataTable();
var dc = (from DataColumn Dc in table.Columns select new { dcc = Dc.ColumnName, Dc.DataType }).ToList();
foreach (var vv in dc)
{
dt.Columns.Add(vv.dcc);
}
foreach(var v1 in table.Rows)
{
dt.Rows.Add(v1);
}
unable to assign row values according to that column names which fetched dynamically from 1st datatable. pls help to get row values inside the final foreach and assign according to that columns. so that next time when i pass 1st datatable with 5 columns and 3 rows it will be easy to complete my requirement.