问题描述
美好的一天!帮助解决问题。
任务。该方法将接收dt表和列表作为输入。在工作表中,必要的列名和列号将传递给我们。
在方法中,我们创建一个包含所需列的表。工作表中定义的列。我创建了一个包含必要列的表。从数据表中,读取列数组。此时,我陷入了困境。我无法在表中添加数组。帮助将dt表中的列数组添加到resTable列。
Good day! Help solve the problem.
Task. The method will receive the dt table and list as input. In the sheet, the necessary column names and column numbers are passed to us.
In the method we create a table with the columns we need. Columns defined in the sheet. I created a table with the necessary columns. From the data table, read the column array. At this point, I stalled. I can not add an array to the table. Help to add an array of columns from the dt table to the resTable column.
/// <summary>
/// The method should return a table with columns needed by the user.
/// </summary>
/// <param name="td"></param>
public DataTable TableFiltreToColl(DataTable dt, List<NumberColumn> list)
{
DataTable resTable = new DataTable();
// in the sheet you need to leave only those columns where the column number is not -1
var listTable = from lt in list
where lt.Number != -1
select lt;
int quantityColl = dt.Columns.Count; //Received an amount of columns in the table transferred to us.
int coll;
string nameColl;
//We create the necessary table with the necessary columns. Columns are given in the resulting sheet.
//The ListTable contains a sheet with the name of the column and the column number in the dt table
foreach (var li in listTable)
{
coll = li.Number;
nameColl = li.Name;
if (coll != -1) // If the column has a value of -1, then this column in the table should not be.
{
DataColumn nameColumn = new DataColumn(nameColl, Type.GetType("System.String"));
resTable.Columns.Add(nameColumn);
}
}
// we created a table with the columns we need. Now you need to write the data in the desired columns.
string str;
int co;
DataRow dataRow = resTable.NewRow();
for (int i = 0; i < resTable.Columns.Count; i++)
{
str = resTable.Columns[i].ColumnName; // get the column name
co = NumColl(str, list); // get the column number of the source table with this column name
var collArray = dt.Rows.Cast<DataRow>().Select(x => x.ItemArray[co]).ToArray(); // Received an array of one column with the number co
!!!!!!!!!!!!!!!! HELP !!!!!!!!!!!!!!!!
// How to set the collArray array to our table?
//dataRow.ItemArray[i] = new object[]{collArray};
}
return resTable;
}
public int NumColl(string str, List<NumberColumn> numColumns)
{
int number = 0;
var name = from n in numColumns
where n.Name == str
select n;
foreach (NumberColumn nc in name)
{
number = nc.Number;
}
return number;
}
public class NumberColumn
{
public int Number { get; set; } // Номер колонки у таблицы
public string Name { get; set; } //Наименование колонки
}
我尝试了什么:
我试图在论坛上找到它互联网,很遗憾我找不到任何东西。
What I have tried:
I tried to find it on the forum and on the Internet, unfortunately I did not find anything.
推荐答案
这篇关于如何将表列的数组插入另一个表的列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!