Adding a column of one data type to another
Hi everyone, I need help sorting a loop for this table, maybe not good for example as an example.
I have 2 datatables, each with different data and different values, the only common value is date. The first table has everything I want except one column of values ββ(from another table). So I need to concatenate this column into the first table and not all other data with it.
Ideally I would like something like:
DataTable tbl1; //Assume both are populated
DataTable tbl2;
tbl1.Columns.Add("newcolumnofdata") //Add a new column to the first table
foreach (DataRow dr in tbl.Rows["newcolumnofdata"]) //Go through each row of this new column
{
tbl1.Rows.Add(tbl2.Rows["sourceofdata"]); //Add data into each row from tbl2 column.
tbl1.Columns["date"] = tbl2.Columns["date"]; //The date field being the same in both sources
}
If anyone can help wud evaluate this, as I said, I just need one column, I don't need to have all the other data. Greetings.
+2
source to share
2 answers
if the second table already has all the rows, but only one column is missing this should be enough to do something like this
DataTable tbl1;
DataTable tbl2;
tbl1.Columns.Add("newCol");
for(int i=0; i<tbl.Rows.Count;i++)
{
tbl1.Rows[i]["newcol"] = tbl2.Rows[i]["newCol"];
tbl1.Rows[i]["date"] = tbl2.Rows[i]["date"];
}
+6
source to share