public void deletePerson(PersonModel personToDelete) { using (var db = new PersonContext()) { db.allPersons.Attach(personToDelete); db.allPersons.Remove(personToDelete); db.SaveChanges(); } }
public PersonModel addPerson(PersonModel personToAdd) { using (var db = new PersonContext()) { db.allPersons.Add(personToAdd); db.SaveChanges(); } syncData(); return personToAdd; }
private void personsGridView_RowLeave(object sender, DataGridViewCellEventArgs e) { if (initialized && added && e.RowIndex == personsGridView.NewRowIndex-1) { //can do validation here PersonModel newPerson = new PersonModel(personsGridView.Rows[e.RowIndex].Cells[1].Value.ToString(), personsGridView.Rows[e.RowIndex].Cells[2].Value.ToString(), personsGridView.Rows[e.RowIndex].Cells[3].Value.ToString(), 0); //Convert.ToInt32(personsGridView.Rows[e.RowIndex].Cells[4].Value)); newPerson = controller.addPerson(newPerson); //MessageBox.Show("New Person Added"); listLengthLabel.Text = controller.getPersons().Count().ToString(); //updateGrid(); personsGridView.Rows[e.RowIndex].Cells[0].Value = newPerson.Id; personsGridView.Rows[e.RowIndex].Cells[1].Value = newPerson.FirstName; personsGridView.Rows[e.RowIndex].Cells[2].Value = newPerson.SecondName; personsGridView.Rows[e.RowIndex].Cells[3].Value = newPerson.TelNumber; personsGridView.Rows[e.RowIndex].Cells[4].Value = newPerson.Age; }//else here for updating }
private void personsGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { DialogResult result = MessageBox.Show("Are you sure you want to delete?", "Sure", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { // controller.getPersons().RemoveAt(e.Row.Index); //find the telephone number of the guy I'm going to delete long? id = (long?)e.Row.Cells[0].Value; PersonModel personToDelete = new PersonModel(); personToDelete.Id = id; controller.deletePerson(personToDelete); // controller.getPersons().RemoveAll(person => person.Id == id); listLengthLabel.Text = controller.getPersons().Count().ToString(); updateGrid(); } else { e.Cancel = true; } }
private void personsGridView_RowValidated(object sender, DataGridViewCellEventArgs e) { try { PersonToUpdate = new PersonModel(); PersonToUpdate.Id = Convert.ToInt64(personsGridView.Rows[e.RowIndex].Cells[0].Value); PersonToUpdate.FirstName = personsGridView.Rows[e.RowIndex].Cells[1].Value.ToString(); PersonToUpdate.SecondName = personsGridView.Rows[e.RowIndex].Cells[2].Value.ToString(); PersonToUpdate.TelNumber = personsGridView.Rows[e.RowIndex].Cells[3].Value.ToString(); PersonToUpdate.Age = (int)personsGridView.Rows[e.RowIndex].Cells[4].Value; controller.updatePerson(PersonToUpdate); } catch (Exception n) { } }
public void findPerson(PersonModel personToFind) { syncData(); PersonModel currentPerson = personsList.Find(person => person.TelNumber == personToFind.TelNumber); }
public void updatePerson(PersonModel personToUpdate) { using (var db = new PersonContext()) { var p = (from person in db.allPersons where person.Id == personToUpdate.Id select person); foreach (var pr in p) { pr.FirstName = personToUpdate.FirstName; pr.SecondName = personToUpdate.SecondName; pr.Age = personToUpdate.Age; pr.TelNumber = personToUpdate.TelNumber; } db.SaveChanges(); } }