/// <summary> /// Deletes data from datagrid and should also update and delete data in datatable /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PeopleDataGrid_PreviewExecuted(object sender, ExecutedRoutedEventArgs e) { PETexamEntities pet = new PETexamEntities(); Persons persons = PeopleDataGrid.SelectedItem as Persons; if (persons != null) { var matchedPerson = (from per in pet.Set <Persons>() where per.ID == persons.ID select per).SingleOrDefault(); if (e.Command == DataGrid.DeleteCommand) { if (e.Command == DataGrid.DeleteCommand) { if (!(MessageBox.Show("Are you sure you want to delete?", "Confirm Delete!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)) { e.Handled = true; } else { pet.Persons.Remove(matchedPerson); pet.SaveChanges(); StatusText.Text = "Success: Selected Data Deleted."; } } } } }
/// <summary> /// Supposed to be a way to create and update changes happending to the DataGrid and copy it over to the DataTable /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PeopleDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) //If changes are commited { PETexamEntities pet = new PETexamEntities(); Persons persons = e.Row.DataContext as Persons; var matchedData = (from per in pet.Set <Persons>() where per.ID == persons.ID select per).SingleOrDefault(); //If user eddits a null table it becomes new data for the table! if (matchedData == null) { //Error Hypothesis //Error may occur because of the syntax (most likely due to error code) // Is it DateOfBirth? Date & Date time is not the same thing. (MOST LIKELY) // DateOfBirth displays MM/dd/yyyy HH:mm:ss which is not how it works in SQL with Date(MM/dd/yyyy) Table <Persons> perTable = pet.Set <Persons>(); Persons _persons = new Persons(); _persons.FirstName = persons.FirstName; _persons.LastName = persons.LastName; _persons.Age = persons.Age; _persons.DateOfBirth = persons.DateOfBirth; _persons.Nationality = persons.Nationality; _persons.HeadShot = persons.HeadShot; _persons.Remarks = persons.Remarks; _persons.GangsID = persons.GangsID; _persons.AgentID = persons.AgentID; perTable.InsertOnSubmit(_persons); perTable.Context.SubmitChanges(); StatusText.Text = "Success: Data Inserted"; } //The only else is if the user eddits a table with existing data //This takes the data and updates it! else { matchedData.FirstName = persons.FirstName; matchedData.LastName = persons.LastName; matchedData.Age = persons.Age; matchedData.DateOfBirth = persons.DateOfBirth; matchedData.Nationality = persons.Nationality; matchedData.HeadShot = persons.HeadShot; matchedData.Remarks = persons.Remarks; matchedData.GangsID = persons.GangsID; matchedData.AgentID = persons.AgentID; pet.SaveChanges(); StatusText.Text = "Success: Data Updated"; } } }