简体   繁体   English

以编程方式将新行添加到 DataTable C#

[英]Add new rows to a DataTable programmatically C#

I have a DataTable which I fill from a database and the in code behind I am trying to add 3 more rows after each row.我有一个从数据库填充的数据表,后面的代码我试图在每行之后再添加 3 行。 Below is the code.下面是代码。 But at the 6th line I get但在第 6 行我得到

Exception of type 'System.OutOfMemoryException' was thrown.抛出了“System.OutOfMemoryException”类型的异常。

  for (int i = 0; i < AlldaysList.Rows.Count; i++)
    {
        DataRow row;
        row = AlldaysList.NewRow();
        DataRow row1;
        row1 = AlldaysList.NewRow();
        DataRow row2;
        row2 = AlldaysList.NewRow();

        // Then add the new row to the collection.
        row["scenarionid"] = DBNull.Value;
        row["description"] = "";
        row1["scenarionid"] = DBNull.Value;
        row1["description"] = "";
        row2["scenarionid"] = DBNull.Value;
        row2["description"] = "";
        AlldaysList.Rows.InsertAt(row, i + 1);
        AlldaysList.Rows.InsertAt(row1, i + 2);
        AlldaysList.Rows.InsertAt(row2, i + 3);
        i++;
    }
//This could be the problem
i < AlldaysList.Rows.Count

i think u should have a variable called int rowCount = AlldaysList.Rows.Count;我认为你应该有一个名为 int rowCount = AlldaysList.Rows.Count; 的变量。 before the loop..在循环之前..

the loop should be  for (int i = 0; i < rowCount; i++)

The reason why i say this is because if u add 3 rows inside the loop ur AlldaysList.Rows.Count is changing by +3 and ur targeting a dynamic variable instead of a static one and so it goes into the loop again and causes an exception..我之所以这么说是因为如果你在循环中添加 3 行,你的 AlldaysList.Rows.Count 正在改变 +3 并且你的目标是动态变量而不是静态变量,所以它再次进入循环并导致异常..

I think you should do something like this:我认为你应该做这样的事情:

int origRowCount = AlldaysList.Rows.Count;
for (int i = 0; i < origRowCount; i++)
{
    for (int j = 1; j <= 3; j++)
    {
        AlldaysList.Rows.InsertAt(MakeNewAlldaysRow(AlldaysList), i * 4 + j);
    }
}

// ....
// (separate method)
static DataRow MakeNewAlldaysRow(DataTable table)
{
    DataRow row = table.NewRow();
    row["scenarionid"] = DBNull.Value;
    row["description"] = "";

    return row;
}

Since the list of rows is going to be increasing, you need to make a note of the row count before you start adding rows.由于行列表将增加,您需要开始添加行之前记下行数。 Also, the insert locations are going to be increasing by 4 , hence i * 4 + j .此外,插入位置将增加4 ,因此i * 4 + j

Generalised version of your code, you can add any number of rows by just changing the value of variable RowsToAdd.代码的通用版本,您可以通过更改变量 RowsToAdd 的值来添加任意数量的行。 You dn't need crate three DataRow variable(row,row1,row2)...你不需要板条箱三个 DataRow 变量(row,row1,row2)...

int RowsToAdd=3
int rowCount = AlldaysList.Rows.Count;
for (int i = 0; i < rowCount; i++)
{
   for (int j = 0; j < RowsToAdd; j++)
   {
     DataRow dr = AlldaysList.NewRow();
     dr["scenarionid"] = DBNull.Value;
     dr["description"] = "";

     AlldaysList.Rows.Add(dr);
   }
}

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

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