Reputation: 47
I want to copy the content from one datatable to another without copying or cloning the data structure. what is the best way to do this? I need to do it this way because I am given a datatable from a SQL server stored procedure and I cannot amend the given read only table. so I need recreate it without copying all the underlying structure. I hope that makes sense.
Regards Amarino
Upvotes: 1
Views: 2385
Reputation: 47
I was looking to do something like this: The below solution works for me.
DataTable lDT2 = someMethod(); //populated with data from SomeMethod()
DataTable lDT3 = new DataTable();
lDT3.Columns.Add("Name", typeof(string));
lDT3.Columns.Add("Name2", typeof(string));
foreach (DataColumn col in lDT2.Columns) {
lDT3.Columns.Add(col.ColumnName, col.DataType);
}
foreach (DataRow dr in lDT2.Rows) {
DataRow lDT3dr = lDT3.NewRow();
for (int i = 0; i < lDT2.Columns.Count; i++) {
if (i == 0) { lDT3dr[i] = "some info"; }
if (i == 1) { lDT3dr[i] = "more info"; }
lDT3dr[i+2] = dr[i];
}
lDT3.Rows.Add(lDT3dr);
}
Upvotes: 1
Reputation: 48139
Another option is to use DefaultView.ToTable(). This also allows you to pull DISTINCT values. If you want all columns
var myNewTable = ExistingDataTable.DefaultView().ToTable();
If you wanted to limit for some criteria
var dv = ExistingDataTable.DefaultView();
dv.Filter = "SomeColumn = 'SomeValue'";
var myFilteredTable = dv.ToTable();
Upvotes: 0
Reputation: 80
If you want to copy dataTable you have two options first one is copy or clone. The second option is using foreach or for loop to copy you can get any columns from dataTable
int columnsize = dt.Columns.Count;
foreach (DataRow X in dt.Rows)
{
for (int i = 0; i < columnsize; i++)
{
// int bir = Convert.ToInt32(X[i]);
// you can set here your need dataTable
}
}
// then here new dataTable you can assing to any datagridview.DataSource = newDataTable
but in the second case you should know ahead what data will come in which place to look this you can get any cell and assing it new DataTable
here is my using code
DataTable dh1 = new DataTable("data1");
dh1.Columns.Add("specification_name", typeof(string));
dh1.Columns.Add("summalar", typeof(string));
dh1.Columns.Add("address", typeof(string));
dh1.Columns.Add("id1", typeof(int));
dh1.Columns.Add("client_id", typeof(int));
dh1.Columns.Add("stack_id", typeof(int));
foreach (DataRow dd in dt.Rows)
{
DataTable temp = new DataTable("temp");
DataRow ttt = dh1.NewRow();
for (int j = 0; j < col; j++)
{
ttt[0] = dd[1];
ttt[1] = dd[2];
ttt[2] = dd[3];
ttt[3] = dd[4];
ttt[4] = dd[5];
ttt[5] = dd[6];
break;
}
dh1.Rows.Add(ttt);
}
Upvotes: 1
Reputation: 58
If I understand correctly you want to copy the data but ignore the schema. You can do this by gathering data set from the source table then iterate through each row and insert it into the destination table.
This is a simple way to copy data from one table to another without any worry of keys or constraints. You will need to make sure your destination table can accept the data (it must be of the correct type or you must cast it to something acceptable).
Upvotes: 0