记录个自己的低级错误!!!!!!!!!!!!!!!!!
今天在对一个表进行删除,发现以下问题:
先附一段代码:(方便调试,加了些MessageBox.)
string deleteID = this.costomerDataGrid[this.costomerDataGrid.CurrentRowIndex, 6].ToString();
this.costomer_RecordTableAdapter.Fill(this.realEstatePDADataSet.Costomer_Record);
MessageBox.Show("过滤前的dataView集合数:"+this.dataView2.Count.ToString());
this.dataView2.RowFilter = "客户编号 = '" +deleteID+ "'";
//this.dataView1.Sort = "来访时间";
MessageBox.Show("过滤条件:" + deleteID);
MessageBox.Show("过滤后的dataView集合数:"+this.dataView2.Count.ToString());
this.costomer_RecordBindingSource.EndEdit();
for (int i = 0; i < this.dataView2.Count; i++)
{
MessageBox.Show("删除第"+i+"条记录");
this.costomer_RecordBindingSource.RemoveAt(i);
}
this.costomer_RecordTableAdapter.Update(this.realEstatePDADataSet.Costomer_Record);
MessageBox.Show("删除成功!");
出现问题:表中的记录没被清除完全。仔细检查,发现Data.Count属性随着循环删除,每次比较 i < this.dataView2.Count,Count属性也是动态变化的。解决办法:
string deleteID = this.costomerDataGrid[this.costomerDataGrid.CurrentRowIndex, 6].ToString();
this.costomer_RecordTableAdapter.Fill(this.realEstatePDADataSet.Costomer_Record);
MessageBox.Show("过滤前的dataView集合数:"+this.dataView2.Count.ToString());
this.dataView2.RowFilter = "客户编号 = '" +deleteID+ "'";
//this.dataView1.Sort = "来访时间";
MessageBox.Show("过滤条件:" + deleteID);
MessageBox.Show("过滤后的dataView集合数:"+this.dataView2.Count.ToString());
this.costomer_RecordBindingSource.EndEdit();
while(this.dataView2.Count!=0)
{
MessageBox.Show("删除第" + Convert.ToSingle(this.dataView2.Count)+ "条记录");
this.costomer_RecordBindingSource.RemoveAt(this.dataView2.Count-1);
}
this.costomer_RecordTableAdapter.Update(this.realEstatePDADataSet.Costomer_Record);
MessageBox.Show("删除成功!");