DataTable将importRow()转换成空表 [英] DataTable importRow() into empty table

查看:116
本文介绍了DataTable将importRow()转换成空表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将excel文档与多张表格合并到一个Datatable中,以便我可以在我的winform应用程序中显示该表格。

I've been trying to merge an excel document with many sheets into a Datatable so that I can display said sheet in my winform app.

从阅读中,我认为Datatable.import(DataRow行)是我最好的选择。因此我的代码如下所示:

From reading around, I figured that Datatable.import(DataRow row) is my best bet. Thus my code looks as follows:

DataTable returnSet = new DataTable();
foreach (DataTable datTab in ds.Tables) // ds is extracted excel sheets in a dataset
{
  foreach (DataRow datRow in datTab.Rows) 
  {
    if (datRow.IsNull(0)) //if empty first col go on to next sheet
    {
      break;
    }
    else
    {
      returnSet.ImportRow(datRow);
    }
  }
}

调试时, datRow / datTab是我预期的,但是在每个ImportRow之后,returnSet仍然是一个空的1x1单元格。对于我所做的错误/缺失的任何洞察将不胜感激。

When debugging, it shows that datRow/datTab is what I expected it to be, however after each ImportRow, returnSet is still an empty 1x1 cell. Any insight as to what I am doing wrong / missing would be greatly appreciated.

推荐答案

我认为原因是因为您的DataTable目前没有模式。您可以尝试克隆原始的DataTable以创建相同的模式(DataColumns等)。

I assume the reason is because your DataTable currently has no schema. You can try to clone the original DataTable to create the same schema(DataColumns etc).

foreach (DataTable datTab in ds.Tables) // ds is extracted excel sheets in a dataset
{
    DataTable tblClone = datTab.Clone();
    foreach (DataRow datRow in datTab.Rows) 
    {

        if (datRow.IsNull(0)) //if empty first col go on to next sheet
        {
            break;
        }
        else
        {
            tblClone.ImportRow(datRow);
        }
    }
}

这篇关于DataTable将importRow()转换成空表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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