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

DataGridView如何只删除选中Checked的项啊?
C# code
                    for (int i = 0; i < dgv_DaoYouGuanLi.Rows.Count; i++)
                    {
                        if (dgv_DaoYouGuanLi.Rows[i].Cells["选择"].Selected==true)
                        {
                            string ZdyId = dgv_DaoYouGuanLi.Rows[i].Cells["ZdyId"].Value.ToString().Trim();
                            string sql = B_XT.delDY(ZdyId);
                            bool flag = TranClass.RunTran(sql);
                            if (flag)
                            {
                                dgv_DaoYouGuanLi.Rows.RemoveAt(dgv_DaoYouGuanLi.Rows[i].Index);
                            }
                            else
                            {
                                MessageBox.Show("删除出现异常,请重新删除!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }



这个不对

------解决方案--------------------
C# code
       for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["选择"];
                if ((Boolean)chk.EditedFormattedValue == true)
                {
 string ZdyId = dgv_DaoYouGuanLi.Rows[i].Cells["ZdyId"].Value.ToString().Trim();
                            string sql = B_XT.delDY(ZdyId);
                            bool flag = TranClass.RunTran(sql);
                            if (flag)
                            {
                                dgv_DaoYouGuanLi.Rows.RemoveAt(dgv_DaoYouGuanLi.Rows[i].Index);
                            }
                            else
                            {
                                MessageBox.Show("删除出现异常,请重新删除!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
  
                }

            }

------解决方案--------------------
删除的话,到这来
for (int i = dataGridView1.Rows.Count-1; i >=0; i--)
------解决方案--------------------
探讨

删除的话,到这来
for (int i = dataGridView1.Rows.Count-1; i >=0; i--)