private void DeleteSubject_Click(object sender, RoutedEventArgs e) { var dg = sender as DataGrid; for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual) { if (vis is DataGridRow) { if (((System.Windows.FrameworkElement)vis).DataContext is DTOSubject) { var subjectId = ((DTO.DTOSubject)((System.Windows.FrameworkElement)vis).DataContext).SubjectId; if (subjectId != -1) { client.DeleteSubject(subjectId); SetListOfSubjects(); } } } } }