1
我想从datagridview中删除多行, 我试过下面的代码,这里的行正在基于索引被删除。如何从datagridview中删除多行而不使用索引?
for (int m = 0; m < dataGridView3.Rows.Count - 1; m++)
{
if (dataGridView3.Rows[m].Cells[2].Value != null)
{
for (int n = 0; n < dataGridView2.Rows.Count - 1; n++)
{
if (dataGridView2.Rows[n].Cells[2].Value != null)
{
if (dataGridView2.Rows[n].Cells[2].Value.Equals(dataGridView3.Rows[m].Cells[2].Value) &&
dataGridView2.Rows[n].Cells[8].Value.Equals(dataGridView3.Rows[m].Cells[8].Value))
{
dataGridView2.Rows.RemoveAt(n);
//break;
}
}
}
}
}
这里的行没有正确删除,因为每次删除后索引都会改变,所以有些记录从循环中丢失。
任何人都可以帮助我如何解决这个问题?
在第二个代码块中调用'ToList()'避免了“集合被修改了”InvalidOperationException。应该工作得很好,除非你的DataGridView有一百万行或... – 2015-02-06 04:06:19
非常感谢Mr.Winney :-)它的工作完美:-) – 2015-02-06 04:16:02
好听。别客气。 :) – 2015-02-06 04:17:28