public void deleteButton_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } var readerRepository = new ReaderRepository(); var dealRepository = new DealRepository(); var bookCopyInDealRepository = new BookCopyInDealRepository(); List <Deal> deals = dealRepository.GetDealsByReaderId(id); List <BookCopyInDeal> bookCopyInDeals = bookCopyInDealRepository.GetBookCopiesInDealByReaderId(id); for (int i = 0; i < bookCopyInDeals.Count; i++) { bookCopyInDealRepository.Remove(bookCopyInDeals[i].Id); } for (int i = 0; i < deals.Count; i++) { dealRepository.Remove(deals[i].Id); } readerRepository.Remove(id); _db.SaveChanges(); SetDataGridView(); MessageBox.Show("Объект удален"); } }