我有一个 for-each 循环,在其中更新了数据行,因此会生成异常 "Collection was modified; enumeration operation might not execute"。有什么方法可以解决吗?我已经尝试使用 To-List 函数,但它不能与数据行一起使用。以下是我的代码:
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;
}
}
for
代替foreach
。 - Parimal Raj