我只想将 List 添加为 DataTable 整行。这是我试过的代码。
private static DataTable _table = new DataTable();
List<string> tempList = new List<string>();
// tempList = {"A1","A2","A3","A4","A5","A6"}
_table.Rows.Add(tempList);
预期输出:
col1|col2 |col3 |col4 |col5| col6
----+-----+-----+------+----+--
row1 A1 | A2 | A3 | A4 | A5 | A6
但是这对我不起作用。它将数据集合插入到第一列。
实际输出:
col1 |col2 |col3 |col4 |col5| col6
----------+-----+-----+------+----+--
row1 A1,A2,A3.| | | | |
请帮助我使用列表添加整行。谢谢
最佳答案
DataRowCollection.Add()
方法需要 Object[]
,因此您应该尝试:
_table.Rows.Add(tempList.ToArray());
关于c# - 使用列表一次将整行添加到 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11328695/