//removes a patient from the db and refreshes ItemsSource of the DataGrid private async void btnDeletePatient_Click(object sender, RoutedEventArgs e) { var result = MessageBox.Show("Вы уверены, что хотите удалить карту пациента и все его обращения?", "Внимание!", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { var currentCard = dgPatients.SelectedItem as PatientCard; if (currentCard != null) { Int32 id = currentCard.Id; ClinicDataRepository clinicRepo = new ClinicDataRepository(); try { clinicRepo.DeleteCard(id); dgPatients.ItemsSource = await clinicRepo.GetPatientCards(); } catch (SqlException) { var err = ConfigurationSettings.AppSettings["dbError"].ToString(); MessageBox.Show(err, "Ошибка"); throw; } catch { MessageBox.Show("Что то пошло не так, приложение будет закрыто", "Ошибка"); throw; } } } }
//loads DataGrids asynchronously. private async void InitializeDataGrids() { //the cts is cancelling async animation task after we are done here CancellationTokenSource cts = new CancellationTokenSource(); if (isInitializingFirstTime) { dgPatients.Visibility = Visibility.Collapsed; lblLoading.Visibility = Visibility.Visible; } try { Animation(cts.Token); ClinicDataRepository clinicRepo = new ClinicDataRepository(); dgPatients.ItemsSource = await clinicRepo.GetPatientCards(); //after adding a new request or after modifying one, //we should refresh the DataGrid to see the changes if (tempCard != null) { dgRequests.ItemsSource = await clinicRepo.GetPatientRequests(tempCard.Id); } } catch (SqlException) { var err = ConfigurationSettings.AppSettings["dbError"].ToString(); MessageBox.Show(err, "Ошибка"); throw; } catch { MessageBox.Show("Что то пошло не так, приложение будет закрыто", "Ошибка"); throw; } if (isInitializingFirstTime) { cts.Cancel(); lblLoading.Content = ""; lblLoading.Visibility = Visibility.Collapsed; dgPatients.Visibility = Visibility.Visible; //don't invoke the loading loop next time isInitializingFirstTime = false; } }