public void InsertEmployee(ListView lvData) { var employeeInsert = new EmployeesInsertUpdate(FormType.Insert); if (employeeInsert.ShowDialog() == DialogResult.OK) { var newEmployee = new Сотрудники { ФИО = employeeInsert.FullName, Телефон = employeeInsert.PhoneNumber, Адрес = employeeInsert.Address, Зарплата = employeeInsert.Salary }; using (var ctx = new DCModel()) { ctx.Сотрудники.Add(newEmployee); ctx.SaveChanges(); } var lvi = new ListViewItem(new[] { newEmployee.ФИО, newEmployee.Телефон, newEmployee.Адрес, newEmployee.Зарплата.ToString() }); lvi.Tag = newEmployee; lvData.Items.Add(lvi); } }
public void EmployeeEdit(ListView lvData) { foreach (ListViewItem selectedItem in lvData.SelectedItems) { var employee = (Сотрудники)selectedItem.Tag; var formEmployeeUpdate = new EmployeesInsertUpdate(FormType.Update, employee.Телефон) { FullName = employee.ФИО, PhoneNumber = employee.Телефон.Trim(' '), Address = employee.Адрес, Salary = employee.Зарплата }; if (formEmployeeUpdate.ShowDialog() == DialogResult.OK) { using (var ctx = new DCModel()) { var id = employee.Код_сотрудника; var original = ctx.Сотрудники.Find(id); if (original != null) { original.ФИО = formEmployeeUpdate.FullName; original.Телефон = formEmployeeUpdate.PhoneNumber.Trim(' '); original.Адрес = formEmployeeUpdate.Address; original.Зарплата = formEmployeeUpdate.Salary; ctx.SaveChanges(); } } } } }