添加一个数据表中的列到另一个 [英] Adding a Column of one datatable to another

查看:118
本文介绍了添加一个数据表中的列到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有需要帮助律分拣这个表了一个循环,似乎无法工作的例子应用到模型,反正这里不言而喻。

Hey all need a lil help sorting a loop for this table out, cant seem to apply a working example to the model, anyway here it goes.

我有2个数据表,每个不同的数据和不同的价值观,共同的唯一的价值是日期。第一个表有我想要的一切在它除了值一列(来自其他表)所以,我需要这个专栏与它合并到第一个表,并不是所有的其它数据。

I have 2 datatables, each with different data and different values, the only value in common is the date. The first table has everything I want in it except a single column of values (from the other table) So I need to merge this column onto the first table, not all the other data with it.

所以,理想情况下,我想的东西,看起来像这样:

So ideally I'd like something that looks like this:

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's column.
    tbl1.Columns["date"] = tbl2.Columns["date"]; //The date field being the same in both sources
}

如果任何人都可以帮忙WUD AP preciate它,就像我说我只需要一列,我并不需要有整个其他DataTable的。干杯。

If anyone can help out wud appreciate it, like I say I just need the one column, I don't need to have the whole of the other datatable. Cheers.

推荐答案

如果第二个表已有的所有行,但只是一列丢失
它应该够做这样的事情。

if the second table already has all rows, but just one column is missing it 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"];
}

这篇关于添加一个数据表中的列到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆