string id_s = ""; for (int i = 0; i < dataGridView1.Rows.Count; i++) //遍历所有行 { if (dataGridView1.Rows[i].Cells["stu_sno"].Value != null) //要获得的学号不能为空 { if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")//是否勾选 { id_s += "," + dataGridView1.Rows[i].Cells["stu_sno"].Value.ToString();//添加学号到字符串 } } } string sqlconn = "server=.;database=student;integrated security=true"; try { id_s = id_s.Substring(1); //截取","号 string sqlcomm = "delete from student_info where stu_sno in (" + id_s + ")"; SqlConnection conn = new SqlConnection(sqlconn); SqlCommand cmd = new SqlCommand(sqlcomm, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); MessageBox.Show("批量删除成功"); } catch (Exception a) { MessageBox.Show(a.ToString()); }