将一个数据表的列添加到另一个

编程入门 行业动态 更新时间:2024-10-28 16:20:34
本文介绍了将一个数据表的列添加到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿,所有这些都需要一个lil帮助排序这个表的循环,似乎没有应用一个工作示例到模型,无论如何在这里它。

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 }

如果有人可以帮助不喜欢它,就像我说我只需要一列,我不需要有其他的数据。干杯。

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"]; }

更多推荐

将一个数据表的列添加到另一个

本文发布于:2023-11-12 00:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!