日期:2014-05-17  浏览次数:20834 次

在panel中循环删除控件延迟的问题,有代码
    for (int i = 0; i < GlobalMembers.SelectedEquipment.Count; i++)
            {
                curequ = (Equipment)GlobalMembers.SelectedEquipment[i];
                curequ.beSelected = false;
                curequ.SetSelect(false);
                GlobalMembers.AllEquipment.Remove(curequ);
                GlobalMembers.GolbalIdAndEuqipment.Remove(curequ.GlobalID);                
                OEquSql = string.Format("delete from OEquipment where Equipment_GlobalID={0}", curequ.GlobalID);
                linkAccess.UpdataOEqu(OEquSql, OEquconn);
                curequ.Dispose();
            }
Equipment是一个自定义的控件,继承picturebox
curequ是panel上的Equipment实例
现在的问题是,当我选择一部分panel上的控件,删除时,采用循环删除,并且从数据库中删除时
所选择的控件是一个一个消失的,有没有办法,让我所要删除的所有控件一次性消失?

------解决方案--------------------
把数据库操作放在循环外
  
string deleteIds=string.Empty;
for (int i = GlobalMembers.SelectedEquipment.Count-1; i >=0; i--)

            {
                curequ = (Equipment)GlobalMembers.SelectedEquipment[i];
                curequ.beSelected = false;
                curequ.SetSelect(false);
                GlobalMembers.AllEquipment.Remove(curequ);
                GlobalMembers.GolbalIdAndEuqipment.Remove(curequ.GlobalID);   
               deleteIds+=curequ.GlobalID+",";             
              
                curequ.Dispose();
            }
 if(deleteIds.Length>0)
 {
     OEquSql = string.Format("delete from OEquipment where Equipment_GlobalID in ({0})", deleteIds.TrimEnd(','));
                linkAccess.UpdataOEqu(OEquSql, OEquconn);
}