c# winform 删除datagridview中的checkbox选定的行

private void indexBtnDelete_Click(object sender, EventArgs e)
        {
            string strNames = “您选择的是:”;
            string strId = “”;
            ArrayList strTotalId = new ArrayList(); //建立一个动态数组,将每次循环条件中符合要求的id值写入
            for (int i = 0; i < this.indexDataGridView1.Rows.Count; i++) //按照datagridview表中的所有行总数循环
            {
                if (this.indexDataGridView1.Rows[i].Cells[0].Value != null) //如果第i行第0列的值不为空
                {
                    if (this.indexDataGridView1.Rows[i].Cells[0].Value.ToString() == “True”) //如果第i行第0列的值为true,也就是checkbox被选中
                    {
                        strNames += this.indexDataGridView1.Rows[i].Cells[3].Value.ToString() + “;”; //将每次选择的第i行第3列的内容,循环写入strNames变量
                        strId = this.indexDataGridView1.Rows[i].Cells[1].Value.ToString(); //将每次第i行第1列的值付给strId,用于写入动态数组
                        strTotalId.Add(strId); //每循环一次,则向动态数组ArrayList中写入值
                    }  
                }
            }
            //弹出提示窗口,点击确定则删除数据,否则取消。
            DialogResult showup = MessageBox.Show(strNames +”您确定要删除吗?”, “系统提示”, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            //下面判断点击确认后,是否有异常,如果有异常则显示添加数据不成功。
            if (showup == DialogResult.OK)
            {
                try
                {
                    foreach (string id in strTotalId)
                    {
                        db.update(“delete from Mn_Pigeons where id = ” + id);
                    }
                    MessageBox.Show(“已成功删除您选择的” + strTotalId.Count +”条数据!”);
                }
                catch
                {
                    MessageBox.Show(“对不起,未成功删除数据!”);
                }

            }
        }

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容