合并两个表而不移动列 [英] Merge two tables without shifting columns

查看:47
本文介绍了合并两个表而不移动列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图用Merge方法合并两个DataTable对象,它似乎表现得这样:

 A = | col1 | col2 | 
| a1 | b1 |
| a2 | b2 |
| a3 | b3 |
| a4 | b4 |



 B = | col3 | col4 | 
| c1 | d1 |
| c2 | d2 |
| c3 | d3 |
| c4 | d4 |



A.Merge(B)给出以下结果:



 | col1 | col2 | col3 | col4 | 
| a1 | b1 | | |
| a2 | b2 | | |
| a3 | b3 | | |
| a4 | b4 | | |
| | | c1 | d1 |
| | | c2 | d2 |
| | | c3 | d3 |
| | | c4 | d4 |



我想要的是:

 | col1 | col2 | col3 | COL4 | 
| a1 | b1 | c1 | d1 |
| a2 | b2 | c2 | d2 |
| a3 | b3 | c3 | d3 |
| a4 | b4 | c4 | d4 |



在我的研究中,我可以找到不同的解决方案,例如创建空列,浏览行并逐个添加它们一个但是第一个,这在性能方面不是一个好方法。



任何人都可以帮忙吗?

解决方案

创建由四列组成的新数据表,然后从两个旧表中插入行......这样就很容易了:



 DataTable dt =  new  DataTable(  AB); 
dt.Columns.Add( col2);
dt.Columns.Add( col2);
dt.Columns.Add( col3);
dt.Columns.Add( col4);

DataRow dr = dt.NewRow();
dr [ col1] = 表A中col1的第一个值
dr [ col2] = 来自表A的col2中的第一个值
dr [ col3] = 来自表B的col3中的第一个值
dr [ < span class =code-string> col4
] = 来自表B的col4中的第一个值

dt.Rows.Add(dr);





肯定你必须添加使用循环不是那样但我给你举个例子


访问这里..





合并表格中的单元格 [ ^ ]

Hi,

I have tried to Merge two DataTable objects with the method Merge and it seems that it behaves this way:

A = |col1|col2|
    |a1  |b1  |
    |a2  |b2  |
    |a3  |b3  |
    |a4  |b4  |


B = |col3|col4|
    |c1  |d1  |
    |c2  |d2  |
    |c3  |d3  |
    |c4  |d4  |


A.Merge(B) gives the following result:

|col1|col2|col3|col4|
|a1  |b1  |    |    |
|a2  |b2  |    |    |
|a3  |b3  |    |    |
|a4  |b4  |    |    |
|    |    |c1  |d1  |
|    |    |c2  |d2  |
|    |    |c3  |d3  |
|    |    |c4  |d4  |


What I want to have is:

|col1|col2|col3|col4|
|a1  |b1  |c1  |d1  |
|a2  |b2  |c2  |d2  |
|a3  |b3  |c3  |d3  |
|a4  |b4  |c4  |d4  |


In my researches, I could find different solutions such as creating empty columns, browsing rows and adding them one by one but first, this is cannot be a good way in term of performance.

Can any one help with this?

解决方案

create new datatable that is made up of the four columns then insert the rows from both old table...it is so easy like this way:

DataTable dt = new DataTable("ab");
dt.Columns.Add("col2");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");

DataRow dr = dt.NewRow();
dr["col1"] = "first value in col1 from table A"
dr["col2"] = "first value in col2  from table A"
dr["col3"] = "first value in col3 from table B"
dr["col4"] = "first value in col4 from table B"

dt.Rows.Add(dr);



for sure you have to add the using loops not like that but i am giving you an example


visit here..


Merging Cells in table[^]


这篇关于合并两个表而不移动列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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