在datatable中追加一行。 [英] Appending a row in datatable.

查看:567
本文介绍了在datatable中追加一行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果datatable为null,则将行复制到datatable,否则需要将该行附加到数据表。

在else部分的此代码中,它应该是附加一行,但在这里添加一行没有任何值。在第一列中,它附加一个文本为[System.Data.DataRow]。



我尝试过:



I have a requirement to check, if datatable is null, then copy the row to datatable otherwise need to append the row to the datatable.
In this code in "else" section it should be appending a row, but here it adding a row without any values in it.In the first column it is appending a text as [System.Data.DataRow].

What I have tried:

if (dtTable == null)
    {
        dtTable = (from DataRow dr in dtDetails.Rows
                   where dr["ID"].ToString() == Id
                   select dr).CopyToDataTable();
    }
    else
    {
       

        dtTable.Rows.Add(from DataRow dr in dtDetails.Rows
        where dr["ID"].ToString() == Id
        select dr)
        // Here in DataTable it adding a new row but without any values.
        // In the first column it is appending a text as [System.Data.DataRow].

    }

推荐答案

通过查看代码,下面的代码返回一个集合(1个或更多)



By looking at the code, the below code is returning a collections (1 or more)

from DataRow dr in dtDetails.Rows
        where dr["ID"].ToString() == Id
        select dr





最好的altrernative,我认为是遍历对象并将行导入dtTable对象。



The best altrernative, I think is to loop through the object and import the row into the dtTable object.

var misteryObject = from DataRow dr in dtDetails.Rows
        where dr["ID"].ToString() == Id
        select dr;

foreach (DataRow dr in misteryObject )
		   {
			 dtTable.ImportRow(dr);
		   }


这篇关于在datatable中追加一行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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