Error in datarow,Collection was modified; enumeration operation might not execute [duplicate]

孤街浪徒 提交于 2019-11-28 03:32:47

问题


I have for-each loop in which the data row is updated so the exception ,Collection was modified; enumeration operation might not execute is generated. any way to fix it? i have seen To-List function but it is not working with data row , here is my code:

foreach (DataRow row in dataTable.Rows) {
  temp = row[0].ToString();
  foreach (DataRow rows in dataTable.Rows) {
    if (temp == rows[0].ToString()) {
      tempdatatable.Rows.Add(row[0],row[1]);
      dataTable.Rows.Remove(rows);
      //Update happens here
    }
    tempdatatable.DefaultView.Sort = "gscitations DESC";
    dataGridView1.DataSource = tempdatatable;
  }
}

回答1:


Try this :

for (int i = 0; i < dataTable.Rows.Count; i++)
{
    var tempRow = dataTable.Rows[i];
    var temp = dataTable.Rows[i][0];
    for (int j = 0; j < dataTable.Rows.Count; j++)
    {
        DataRow rows = dataTable.Rows[j];
        if (temp == rows[0].ToString())
        {
            tempdatatable.Rows.Add(tempRow[0], tempRow[1]);
            dataTable.Rows.Remove(rows);      //Update happen here
        }
        tempdatatable.DefaultView.Sort = "gscitations DESC";
        dataGridView1.DataSource = tempdatatable;
    }
}



回答2:


You cannot modify collection while enumerating it using Enumerator, which is happening behind the scene of the foreach statement (MDSN link).

One possible way to solve this problem is to collect rows to be deleted in the first enumeration and than remove them in the separate loop like this:

var rowsToDelete = new List<DataRow>();

foreach (DataRow row in dataTable.Rows)
     {
         temp = row[0].ToString();
         foreach (DataRow rows in dataTable.Rows)
         {
             if (temp == rows[0].ToString())
             {
                 tempdatatable.Rows.Add(row[0],row[1]);
                 rowsToDelete.Add(rows);
             }
             tempdatatable.DefaultView.Sort = "gscitations DESC";
             dataGridView1.DataSource = tempdatatable;
         }
     }

rowsToDelete.ForEach( x => dataTable.Rows.Remove(x) );

You can also replace foreach loop with for, but you need to do extra work properly handling the current index while deleting the elements.




回答3:


I would say that you should make a separate table of entries, and instead of calling datatable.Rows.Remove(rows), add the row "rows" to this other table. Then, whenever row or rows iterates, you run an if statement to check if its been "deleted", i.e., in the list of deleted rows. After the enumeration is over, you can then delete those rows permanently from the table.

EDIT:

Here's the code implementation:

DataTable duplicates = dataTable;
duplicates.Rows.Clear(); /* Produces an empty duplicate of the 
dataTable table to put the duplicates in */
foreach (DataRow row in dataTable.Rows)
{
     if (!duplicates.Rows.Contains(row))
     {    
         temp = row[0].ToString();
         foreach (DataRow rows in dataTable.Rows)
         {
             if (temp == rows[0].ToString()&&!duplicates.Rows.Contains(rows)) //need unique key
             {
                 tempdatatable.Rows.Add(row[0],row[1]);

             }
             tempdatatable.DefaultView.Sort = "gscitations DESC";
             dataGridView1.DataSource = tempdatatable;
         }
    }
}
foreach (DataRow row in duplicates.Rows)
{
    dataTable.Rows.Remove(row);
}

if you don't have a unique key, you can try switching !duplicates.Rows.Contains(/*specific row*/) for duplicates.Rows.IndexOf(/*specific row*/)>0. That should provide an adequate substitute.



来源:https://stackoverflow.com/questions/15457571/error-in-datarow-collection-was-modified-enumeration-operation-might-not-execut

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!