1 //删除,行删除 2 private void 删除_Click(object sender, EventArgs e) 3 { 4 5 int dgrcount = dataGridView1.SelectedRows.Count; 6 if (dgrcount>0) 7 { 8 string selectedRowName=dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取选中的行的用户名 9 10 string cmdText = "delete from tb_admin where 用户名='" + selectedRowName + "'";//构建删除行语句 11 OleDbConnection conn = new OleDbConnection(); 12 conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=ErLake.mdb"; 13 OleDbCommand cmd = new OleDbCommand(cmdText, conn); 14 conn.Open(); 15 //逐行删除 16 foreach (DataGridViewRow dgvRow in dataGridView1.SelectedRows) 17 { 18 19 dataGridView1.Rows.Remove(dgvRow);//删除dataGridView中的数据 20 cmd.ExecuteNonQuery();//删除数据库表中的数据 21 } 22 conn.Close(); 23 24 25 } 26 27 }