c# - 使用列表一次将整行添加到 DataTable

标签 c# list datatable

我只想将 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/

相关文章:

c# - 空引用类型变量会影响垃圾收集器 (GC) 性能吗?

c# - ObjectListView Treeview 获取选中的 TreeView 节点Id

c++ - 结构的 char* 成员被 strcmp 覆盖?

css - 覆盖数据表 css

c# - 从azure存储异步下载blob并将其保存在DataTable中

c# - 使用 System.Data 而不是 System.Data.SqlClient

c# - 在 C# 中更改网格的 Canvas.ZIndex

python - 需要从 python 列表中找到子元素

python - 将数据帧值转换为一维列表

c# - 将数据表从一个 aspx 页面传输到另一个 C#