当前位置:编程学习 > C#/ASP.NET >>

Datagridview怎样批量删除

 #region 删除按钮事件
        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int selectCount = 0;
                int checkIndes = -1;
                int rowIndex = -1;
                //循环到你选择的checkbox
                foreach (DataGridViewRow item in gridUserList.Rows)
                {
                    rowIndex++;
                    if (item.Cells["chk"].Value != null && (bool)item.Cells["chk"].Value)
                    {
                        selectCount++;
                        if (checkIndes == -1)
                        {
                            checkIndes = rowIndex;
                        }
                    }
                }
                if (selectCount == 0)
                {
                    CommonFun.Message_ExclamationOk("请选择要删除信息的用户");
                    return;
                }
                //当选择的数量大于等于2个时候,给予提示
                if (selectCount >= 2)
                {
                    CommonFun.Message_ExclamationOk("一次只能删除一条记录");
                    return;
                }

                 DialogResult res = CommonFun.Message_QuestionYesNo(String.Format("确定删除该用户?"));
                 if (res == DialogResult.Yes)
                 {
                     //把选择的id的值传给User_id
                     string User_id = gridUserList.Rows[checkIndes].Cells["USER_ID"].Value.ToString();
                     BLL_MS_USER user = new BLL_MS_USER();
                     //调用删除方法
                     user.Delete(User_id);
                     gridUserList.Rows.RemoveAt(checkIndes);

                     CommonFun.Message_Information("删除成功");
                 }
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,