private void bntHuyLop_Click(object sender, RoutedEventArgs e) { try { index = dataGridDanhSachMonHoc.SelectedIndex; Lop obj = dataGridDanhSachLop.SelectedItem as Lop; LopDAO dao = new LopDAO(); if (dao.Remove(obj.MaLop)) { MessageBox.Show("Đã xóa", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information); Reload(); dataGridDanhSachMonHoc.SelectedIndex = index; } //if (obj != null) //{ // foreach (var lop in obj) // { // index = dataGridDanhSachMonHoc.SelectedIndex; // Lop mlop = db.Lop.Find((lop as Lop).MaLop); // List<ThamGia> listThamGia = db.ThamGia.Where(x => x.MaLop == mlop.MaLop).ToList(); // foreach (var tg in listThamGia) // { // ThamGia mtg = tg as ThamGia; // if (String.IsNullOrEmpty(mtg.MaLop) == false) // { // db.ThamGia.Remove(db.ThamGia.Find(mtg.MaLop,mtg.MaSinhVien)); // db.SaveChanges(); // } // } // db.Lop.Remove(mlop); // db.SaveChanges(); // } // MessageBox.Show("Đã xóa","Thông báo",MessageBoxButton.OK,MessageBoxImage.Information); // Reload(); // dataGridDanhSachMonHoc.SelectedIndex = index; //} } catch (Exception ex) { MessageBox.Show(ex.Message); } }