.net c#如何将数据表列和值复制到另一个数据表

how to copy datatable column and value to another datatable
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我有一个有 0 行的 4 列(名称、地址、联系人、标记)的数据表 dt1,我有另一个有 2 列(StdName、StdAddress)的数据表 dt2,只有几行.

当我做类似 dt1 = dt2.copy(); 那么我的 dt1 正在发生变化.我不想要的.

我想将 dt2 列和值复制到 dt1.所以在我的 dt1 的列(名称和地址)中将填充 stdName 和 StdAddress 值.

请有人帮助我如何做到这一点.

解决过程1

我试过这个解决方案

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");

它帮助我解决我的问题.

解决过程2

尝试这个:

  for (int i = 0; i < dt2.Rows.Count; i++)
  {
    dt1.Rows.Add(dt2.Rows[i][0], dt2.Rows[i][1]);
  }
解决过程3
  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"];
  }
解决过程4

尝试这个.

 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.
解决过程5

尝试一下....

    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);
解决过程6

我知道这是旧的,但数据来自哪里?

如果您正在查询 sql 数据库,您可以更改:

    SELECT <column1>, <column2> FROM...

    SELECT <column2>, <column1> FROM...

查询并返回包含您想要的列的数据表.

解决过程7

我不确定,但你可以试试这个......

Datatable1.Merge(Datable2)

它会给你这样的输出

解决过程8
  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"];
  }

这对我来说非常有效.我只需要像这样在 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
解决过程9

您可以克隆对表的引用并通过调用 clear 删除行,如下所示:

NewDataTable= OldDatatable.Clone();
NewDataTable.rows.clear();

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.

when I am doing something like dt1 = dt2.copy(); then my dt1 is getting change. which I don't want.

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.

Please someone help me how to do that.

Solutions1

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.

Solutions2

Try this:

  for (int i = 0; i < dt2.Rows.Count; i++)
  {
    dt1.Rows.Add(dt2.Rows[i][0], dt2.Rows[i][1]);
  }
Solutions3
  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"];
  }
Solutions4

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.
Solutions5

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);
Solutions6

I know this is older, but where is the data coming from?

If you are querying a sql database, you can just change:

    SELECT <column1>, <column2> FROM...

    SELECT <column2>, <column1> FROM...

Query and return a datatable with the columns like you want them.

Solutions7

I am not sure but you can try this....

Datatable1.Merge(Datable2)

It will give you output like this

Solutions8
  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:

  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
Solutions9

You can clone the Reference to the Table and delete Rows by call clear like so:

NewDataTable= OldDatatable.Clone();
NewDataTable.rows.clear();
转载于:https://stackoverflow.com/questions/15239723/how-to-copy-datatable-column-and-value-to-another-datatable

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入