List selectedRows = (from row in DataGridViewFamille.Rows.Cast() where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true select row).ToList(); if (MessageBox.Show(string.Format("Do you want to delete {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes) { //delete Nature de Charge before delete Famille foreach (DataGridViewRow row in selectedRows) { try { using (SqlConnection con = new SqlConnection(connstring)) { con.Open(); using (SqlCommand command = new SqlCommand("DELETE NatureCharge FROM NatureCharge n INNER JOIN Famille f on n.IdFam = f.IdFam WHERE IdNat = @IdNat", con)) { command.Parameters.AddWithValue("@IdFam", row.Cells["IdFam"].Value); command.ExecuteNonQuery(); } con.Close(); } } catch (SystemException ex) { MessageBox.Show(string.Format("An error occurred: {0}", ex.Message)); } } //Delete Famille foreach (DataGridViewRow row in selectedRows) { using (SqlConnection con = new SqlConnection(connstring)) { using (SqlCommand cmd = new SqlCommand("DELETE FROM Famille WHERE IdFam = @IdFam", con)) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@IdFam", row.Cells["IdFam"].Value); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } //Update the datagridview this.BindGrid(); }