Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复制数据表列';将值放入另一个数据表行C中#_C#_Loops_Foreach_Datatable - Fatal编程技术网

C# 复制数据表列';将值放入另一个数据表行C中#

C# 复制数据表列';将值放入另一个数据表行C中#,c#,loops,foreach,datatable,C#,Loops,Foreach,Datatable,我有两个数据表,两个数据表都由七列组成。我想将第一个数据表的列值复制到第二个数据表行中。源表的行不能大于7行 比如说 Source Destination SourceColumn ColumnOne ColumnTwo ColumnThree ColumnFour ...... 1 1 2 3 4 2 3 4 6 7

我有两个数据表,两个数据表都由七列组成。我想将第一个数据表的列值复制到第二个数据表行中。源表的行不能大于7行

比如说

Source          Destination 
SourceColumn    ColumnOne   ColumnTwo    ColumnThree    ColumnFour ......
   1              1           2            3                4
   2
   3
   4 
   6
   7
我已经找到了这个函数,但它没有按预期工作

   private void CopyColumns(DataTable Source, DataTable Destination, params string[] Columns )
    {
        foreach(DataRow SourceRow in dtable.Rows)
        {
            DataRow DestinationRow = dt.NewRow();
            foreach(string ColumnName in Columns)
            {
                DestinationRow[ColumnName] = SourceRow[ColumnName];
            }
            dt.Rows.Add(DestinationRow);
        }
    }

您知道如何将每个值移动到目标表中相应的列吗?

下面是示例代码。这里
dt1
dt2
分别是源表和目标表

假设
dt1
的行数与
dt2
中的列数相同

var newRow = dt2.NewRow();  //dt2 is the destination table. Creating new row for destination table.

for (var i = 0;i < dt2.Columns.Count;i++)
{
    var row1 = dt1.Rows[i];
    newRow[i] = row1[0];
}

dt2.Rows.Add(newRow); //Adding new row to the destination table.

var xRow = dt2.Rows[0]; //Retrieving row for displaying the data to Console.

for (var j = 0; j < dt2.Columns.Count; j++)
{
    Console.WriteLine(xRow[j]);
}
var newRow=dt2.newRow()//dt2是目标表。正在为目标表创建新行。
for(var i=0;i
以下是示例代码。这里
dt1
dt2
分别是源表和目标表

假设
dt1
的行数与
dt2
中的列数相同

var newRow = dt2.NewRow();  //dt2 is the destination table. Creating new row for destination table.

for (var i = 0;i < dt2.Columns.Count;i++)
{
    var row1 = dt1.Rows[i];
    newRow[i] = row1[0];
}

dt2.Rows.Add(newRow); //Adding new row to the destination table.

var xRow = dt2.Rows[0]; //Retrieving row for displaying the data to Console.

for (var j = 0; j < dt2.Columns.Count; j++)
{
    Console.WriteLine(xRow[j]);
}
var newRow=dt2.newRow()//dt2是目标表。正在为目标表创建新行。
for(var i=0;i
如果第一个表包含的行数超过7行怎么办?或者根本就不是这样?基本上,您想将行转换为列?对不起,我忘了提到源表的行不能超过7行,就像您实际上只需要一个循环一样?-)是的,我需要将源表值的列转换为目标表@TaWWhat的行,如果第一个表包含的行数超过7行怎么办?或者根本就不是这样?基本上,您想将行转换为列?对不起,我忘了提到源表的行不能超过7行,就像您实际上只需要一个循环一样?-)是的,我需要将源表值的列转换为目标表的行@TaWThank。我已经对您的代码片段做了一些更改,它可以正常工作。非常感谢您。我在您的代码片段中做了一些更改,它按预期工作