datagridview一次性移除多行选中数据,同时删除数据库数据
源码:
private void btnDel_Click(object sender, EventArgs e) { DialogResult RSS = MessageBox.Show(this, "确定要删除选中行数据码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); switch (RSS) { case DialogResult.Yes: for (int i = this.dataGridView1.SelectedRows.Count; i > 0; i--) { int ID = Convert.ToInt32(dataGridView1.SelectedRows[i - 1].Cells[0].Value); dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index); //调用后台删除方法,删除数据库数据 Facade.AlterOperatorFa delOperator = new Facade.AlterOperatorFa(); Entity.userEntity userInfo = new Entity.userEntity(); userInfo.userID = ID.ToString().Trim(); delOperator.DelOpe(userInfo); } break; case DialogResult.No: break; } }
很好用,就是一点不方便,这么写只能用于删除datagridview绑定数据源的时候,当我们把List集合的值赋给datagridview显示再用以上代码删除就会报如下错误
所以,这个时候代码就得改写了,
在List中删除数据,后重新绑定控件,
因为还要求删除数据库中数据,所以,直接获取选中行的ID,删除数据库中数据,重新返回List赋给datagridview完事儿
private void btnDel_Click(object sender, EventArgs e) { DialogResult RSS = MessageBox.Show(this, "确定要删除选中行数据码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); switch (RSS) { case DialogResult.Yes: for (int i = this.dataGridView1.SelectedRows.Count; i > 0; i--) { int ID = Convert.ToInt32(dataGridView1.SelectedRows[i - 1].Cells[0].Value); //调用后台删除方法,删除数据库数据 Facade.AlterOperatorFa delOperator = new Facade.AlterOperatorFa(); Entity.userEntity userInfo = new Entity.userEntity(); userInfo.userID = ID.ToString().Trim(); delOperator.DelOpe(userInfo); } comboBox1_SelectedIndexChanged(sender, e);//for循环里删除了一句话,在此加了这行语句 break; case DialogResult.No: break; } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedIndex == 0) { //level是1的 Facade.QueryOperatorInfo opeFa1 = new Facade.QueryOperatorInfo(); List<userEntity> userList1 = new List<userEntity>(); Entity.userEntity userInfo1 = new userEntity(); userInfo1.userLevel = "1"; userList1 = opeFa1.getUsers(userInfo1); //level是2的 Facade.QueryOperatorInfo opeFa2 = new Facade.QueryOperatorInfo(); List<userEntity> userList2 = new List<userEntity>(); Entity.userEntity userInfo2 = new userEntity(); userInfo2.userLevel = "2"; userList2 = opeFa2.getUsers(userInfo2); //合并level==1和level==2的list集合 List<userEntity> resultEnd = userList1.Concat(userList2).ToList(); dataGridView1.DataSource = (from p in resultEnd select new { 用户ID = p.userID, 用户级别 = p.userLevel }).ToList(); } else if (comboBox1.SelectedIndex == 1) { Facade.QueryOperatorInfo opeFa = new Facade.QueryOperatorInfo(); List<userEntity> userList = new List<userEntity>(); Entity.userEntity userInfo = new userEntity(); userInfo.userLevel = "2"; userList = opeFa.getUsers(userInfo); dataGridView1.DataSource = (from p in userList select new { 用户ID = p.userID, 用户级别 = p.userLevel }).ToList(); } else if (comboBox1.SelectedIndex == 2) { Facade.QueryOperatorInfo opeFa = new Facade.QueryOperatorInfo(); List<userEntity> userList = new List<userEntity>(); Entity.userEntity userInfo = new userEntity(); userInfo.userLevel = "1"; userList = opeFa.getUsers(userInfo); dataGridView1.DataSource = (from p in userList select new { 用户ID = p.userID, 用户级别 = p.userLevel }).ToList(); } }
就是下面这个窗体!