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