C# .Net数据表列导出
我希望从DataTable对象中获取一列及其所有行数据,然后在另一个数据表中创建一个新列,并用新列及其行将其追加。我一直遇到的问题是,行数据会显示出来,列名也会显示出来,但所有行都附加到同一列中,我确信我遗漏了一些明显的东西,事实上我知道我是!任何帮助都是非常感激的C# .Net数据表列导出,c#,csv,datatable,split,append,C#,Csv,Datatable,Split,Append,我希望从DataTable对象中获取一列及其所有行数据,然后在另一个数据表中创建一个新列,并用新列及其行将其追加。我一直遇到的问题是,行数据会显示出来,列名也会显示出来,但所有行都附加到同一列中,我确信我遗漏了一些明显的东西,事实上我知道我是!任何帮助都是非常感激的 void GetColumns() { // add the columns to the new datatable foreach (int i in mapValues)
void GetColumns()
{
// add the columns to the new datatable
foreach (int i in mapValues)
{
SplitData.Columns.Add(i.ToString());
}
// map values contains the index numbers of my target columns
foreach (int x in mapValues)
{
foreach (DataRow row in OrigData.Rows)
{
SplitData.Rows.Add(row[mapValues[LocalIndex]]);
}
LocalIndex++;
}
}
您使用的DataRow.Add重载是params重载,因此您只需将原始列数据放入新DataTable的第一列 您可能需要以下内容:
DataRow newRow = SplitData.NewRow(); // gets a new blank row with the right schema
newRow[x.ToString()] = row[mapValues[LocalIndex]; // sets the column (that you created before) to the orig data
SplitData.Rows.Add(newRow);
作为第二个for循环的核心。你也可以在一个循环中完成它。虽然公认的答案完全正确,我从中学到了一些东西,但事实证明DataTable.ImportRow方法正是我所需要的,因此仅供将来可能遇到此问题的任何人参考。请发布(概述)你的代码。这就是问题所在,我知道我需要以某种方式在列和行之间迭代,尽管我还没有看到一个明确的方法来做到这一点……干杯,伙计,我知道这很简单