C# 如何将数据表列和值复制到另一个数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15239723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to copy datatable column and value to another datatable
提问by Rocky
I have a datatable dt1 with 4 column(Name,Address,Contact,Marks) which has 0 Rows, and I have another datatable dt2 with 2 column(StdName, StdAddress) with few Rows.
我有一个有 0 行的 4 列(名称、地址、联系人、标记)的数据表 dt1,我有另一个有 2 列(StdName、StdAddress)的数据表 dt2,只有几行。
when I am doing something like dt1 = dt2.copy(); then my dt1 is getting change. which I don't want.
当我做类似 dt1 = dt2.copy(); 那么我的 dt1 正在发生变化。我不想要的。
I want to copy dt2 column and value to dt1. so in my dt1's column(Name and Address) will fill with stdName and StdAddress Value.
我想将 dt2 列和值复制到 dt1。所以在我的 dt1 的列(名称和地址)中将填充 stdName 和 StdAddress 值。
Please someone help me how to do that.
请有人帮助我如何做到这一点。
采纳答案by Rocky
I tried with this solution
我试过这个解决方案
private void CopyColumns(DataTable source, DataTable dest, params string[] columns)
{
foreach (DataRow sourcerow in source.Rows)
{
DataRow destRow = dest.NewRow();
foreach(string colname in columns)
{
destRow[colname] = sourcerow[colname];
}
dest.Rows.Add(destRow);
}
}
CopyColumns(source, destiny, "Column1", "column2");
It help me to solve my problem.
它帮助我解决我的问题。
回答by Manoj Savalia
Try this.
尝试这个。
DataTable dtEmp = new DataTable("EMP");
dtEmp.Columns.Add("Name", typeof(String));
dtEmp.Columns.Add("Address", typeof(String));
dtEmp.Columns.Add("Contact", typeof(String));
dtEmp.Columns.Add("Marks", typeof(String));
DataTable dtStd = new DataTable("STD");
dtStd.Columns.Add("StdName", typeof(String));
dtStd.Columns.Add("StdAddress", typeof(String));
foreach (DataColumn dc in dtStd.Columns)
{
dtEmp.Columns.Add(dc.ColumnName, dc.DataType);
}
// Import rows from dtStd Table.
回答by Snippet
for (int k = 0; k < dt2.Rows.Count; k++)
{
dt1.Rows[k]["Name"] = dt2.Rows[k]["StdName"];
dt1.Rows[k]["Address"] = dt2.Rows[k]["StdAddress"];
}
回答by iJay
Try this:
尝试这个:
for (int i = 0; i < dt2.Rows.Count; i++)
{
dt1.Rows.Add(dt2.Rows[i][0], dt2.Rows[i][1]);
}
回答by gourishankar
try it....
尝试一下....
DataTable dt1 = new DataTable();
dt1.Columns.Add("col1", typeof(String));
dt1.Columns.Add("col2", typeof(String));
dt1.Columns.Add("col3", typeof(String));
dt1.Columns.Add("col4", typeof(String));
DataTable dt2 = new DataTable();
dt2.Columns.Add("col5", typeof(String));
dt2.Columns.Add("col6", typeof(String));
dt1.Merge(dt2);
回答by jdowd7
I know this is older, but where is the data coming from?
我知道这是旧的,但数据来自哪里?
If you are querying a sql database, you can just change:
如果您正在查询 sql 数据库,您可以更改:
SELECT <column1>, <column2> FROM...
SELECT <column2>, <column1> FROM...
Query and return a datatable with the columns like you want them.
查询并返回包含您想要的列的数据表。
回答by Tejas Sawant
I am not sure but you can try this....
我不确定,但你可以试试这个......
Datatable1.Merge(Datable2)
It will give you output like this
它会给你这样的输出
回答by MrLatePass
for (int k = 0; k < dt2.Rows.Count; k++) {
dt1.Rows[k]["Name"] = dt2.Rows[k]["StdName"];
dt1.Rows[k]["Address"] = dt2.Rows[k]["StdAddress"];
}
This worked excellent for me. I just had to rewrite it in VB like this:
这对我来说非常有效。我只需要像这样在 VB 中重写它:
For r = 0 To t2.Rows.Count - 1
t1.Rows(r)("Stat") = t2.Rows(r)("Stat")
t1.Rows(r)("Dest") = t2.Rows(r)("Dest")
t1.Rows(r)("Orig") = t2.Rows(r)("Orig")
Next
回答by nanda9894
You can clone the Reference to the Table and delete Rows by call clear like so:
您可以克隆对表的引用并通过调用 clear 删除行,如下所示:
NewDataTable= OldDatatable.Clone();
NewDataTable.rows.clear();