简体   繁体   English

如何在 C# .net 中的数据表中添加特定数量的行?

[英]How to add particular number of rows to a datatable in C# .net?

I have 'n' number of rows in datatable1.我在datatable1中有'n'行。 Now i want add 'n' number of rows to datatable2.Its notable that two datatables are having different number of columns.Can anyone please help me with c# coding?现在我想向datatable2添加'n'行数。值得注意的是两个数据表具有不同的列数。有人可以帮我处理c#编码吗?

I have used following code but it doesn't work.我使用了以下代码,但它不起作用。

            datatable1 = feeCompBLL.getcommunity();
            foreach (DataRow drow in datatable1.Rows)
            {
                DataRow table2rows = datatable2.NewRow();
                datatable2.Rows.Add(table2rows );

            }

You, can use datatable2 = datatable1.Copy();你,可以使用datatable2 = datatable1.Copy(); to do ur work.做你的工作。 Hope it helps.希望能帮助到你。

if the two datatables are having different number of columns you should specify the particular colum that you are trying to insert into the "datatable2"如果两个数据表的列数不同,则应指定您尝试插入“datatable2”的特定列

eg例如

foreach (DataRow drow in datatable1.Rows)
{
  DataRow table2rows = datatable2.NewRow();                
  table2rows[columname] = drow[columnname];
  datatable2.Rows.Add(table2rows);            
}

that is you will have to assign the correct column name or column number to the new row.也就是说,您必须为新行分配正确的列名或列号。

To help you I need some info on the code;为了帮助你,我需要一些关于代码的信息; 1. does datatable2 have columns defined 2. What is FeeSetupRow 1. datatable2 是否定义了列 2. 什么是 FeeSetupRow

If columns are added already, I fee, datatable2.Rows.Add(FeeSetupRow) should be like datatable2.Rows.Add(table2rows).如果已经添加了列,我认为 datatable2.Rows.Add(FeeSetupRow) 应该像 datatable2.Rows.Add(table2rows)。

If you want to copy also the data from datatable1 to datatable2 then you follow as kalyan specified.如果您还想将数据从 datatable1 复制到 datatable2,那么您按照 kalyan 指定的方式进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM