使用列表一次将整行添加到DataTable [英] Add entire row to DataTable at once using list

查看:58
本文介绍了使用列表一次将整行添加到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将List添加为DataTable的整行。这是我尝试过的代码。

I just want to add List as a DataTable entire row. here is the code which I have tried.

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

但是这对我不起作用。

However this is not working for me. It will insert data collection to first column.

实际输出:

      col1      |col2 |col3 |col4  |col5| col6
      ----------+-----+-----+------+----+--
row1   A1,A2,A3.|     |     |      |    |  

请帮助我使用列表添加整行。谢谢

Please help me to Add entire row using list. thank you

推荐答案

DataRowCollection.Add() 方法需要 Object [] ,因此您应该尝试:

DataRowCollection.Add() Method expects Object[], so you should probably try:

_table.Rows.Add(tempList.ToArray());

这篇关于使用列表一次将整行添加到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆