/// <summary>
/// 移除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
if(this.dataGridView2.RowCount>0)
{
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (row.Cells["xuanze1"].Value != null)
{
if (row.Cells["xuanze1"].Value.ToString() == "True")
{
this.dataGridView2.Rows.Remove(row); }
}
}
}
}
删除的数据老是对不上,麻烦高手帮我看看
{
if (row.Cells["xuanze1"].Value != null)
{
if (row.Cells["xuanze1"].Value.ToString() == "True")
{
this.dataGridView2.Rows.Remove(row);
}
}
}
这种写法带来的问题就是foreach是只读的,你把其中一个删除了,所以下次再进行的时候,他的循环总数都发生变化了,能准确吗?
因为你删除第一行时,datagridview已经发生变化,那么foreach主体已经变化了。应该改为for循环,并且是从后往前 for (int i=10;i>=10;i--)
DataGridViewSelectedRowCollection temp_del_row_array = this.dataGridView1.SelectedRows; foreach (DataGridViewRow item in temp_del_row_array)
{
this.dataGridView1.Rows.Remove(item);
}