private void Button_DeleteDepartment_Click(object sender, EventArgs e) { GlobalVars.Log.Info("MainForm: Удаление отдела"); using (var db = new TestDBEntities()) { var selectedDepartmentId = new Guid(TreeView_Departments.SelectedNode.Name); var selectedDepartment = db.Department.First(x => x.ID == selectedDepartmentId); if (MessageBox.Show($"Вы действительно хотите удалить отдел \"{selectedDepartment.Code} / {selectedDepartment.Name}\"", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { var departmentsDorDelete = new List <Department>(); RecursiveDeleteDepartmentEmployers(departmentsDorDelete, selectedDepartment, db); db.Department.RemoveRange(departmentsDorDelete); db.SaveChanges(); FillDepartmentsTreeView(); DataGridView_Employers.DataSource = null; DataGridView_Employers.Columns.Clear(); } } }
private void Button_RemoveEmployee_Click(object sender, EventArgs e) { GlobalVars.Log.Info("MainForm: Удаление сотрудника"); if (DataGridView_Employers.SelectedRows.Count != 0) { var employeeId = Convert.ToDecimal(DataGridView_Employers.SelectedRows[0].Cells["ID"].Value); using (var db = new TestDBEntities()) { var employee = db.Empoyee.First(x => x.ID == employeeId); if (MessageBox.Show($"Вы действительно хотите удалить сотрудника {employee.FirstName} {employee.SurName} {employee.Patronymic} из базы?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { db.Empoyee.Remove(employee); db.SaveChanges(); FillDataGridViewEmployers(); } } } }
private void Button_Save_Click(object sender, EventArgs e) { GlobalVars.Log.Info("EmployeeInfoEdit: Сохранение информации о сотруднике"); if (TextBox_FirstName.Text == "" || TextBox_SurName.Text == "" || TextBox_Position.Text == "" || ComboBox_Department.SelectedIndex == -1) { MessageBox.Show("Не все обязательные поля заполнены!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (DateTimePicker_BirthDate.Value > DateTime.Now) { MessageBox.Show("Дата рождения не может быть больше текущей даты!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { try { using (var db = new TestDBEntities()) { var departments = db.Department.ToList(); if (_employeeId == -1) { db.Empoyee.Add(new Empoyee { FirstName = TextBox_FirstName.Text, SurName = TextBox_SurName.Text, Patronymic = TextBox_Patronymic.Text, DateOfBirth = DateTimePicker_BirthDate.Value, DocSeries = TextBox_DocSeries.Text, DocNumber = TextBox_DocNumber.Text, Position = TextBox_Position.Text, DepartmentID = departments[ComboBox_Department.SelectedIndex].ID }); } else { var employee = db.Empoyee.First(x => x.ID == _employeeId); employee.FirstName = TextBox_FirstName.Text; employee.SurName = TextBox_SurName.Text; employee.Patronymic = TextBox_Patronymic.Text; employee.DateOfBirth = DateTimePicker_BirthDate.Value; employee.DocSeries = TextBox_DocSeries.Text; employee.DocNumber = TextBox_DocNumber.Text; employee.Position = TextBox_Position.Text; employee.DepartmentID = departments[ComboBox_Department.SelectedIndex].ID; } db.SaveChanges(); } this.Close(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { GlobalVars.Log.Info(ex, "EmployeeInfoEdit: Ошибка при сохранении информации о сотруднике"); MessageBox.Show($"Возникла ошибка. Возможно некоторые поля заполнены неправильно.\n\n{ex.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void Button_Save_Click(object sender, EventArgs e) { GlobalVars.Log.Info("DepartmentInfoEditForm: Соранение информации об отделе."); if (MaskedTextBox_ID.Text == "" || TextBox_Name.Text == "") { MessageBox.Show("Не все обязательные поля заполнены!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (!Guid.TryParse(MaskedTextBox_ID.Text, out var res)) { MessageBox.Show("Идентификатор введен неправильно!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (ComboBox_ParentDepartment.SelectedIndex == -1) { MessageBox.Show("Отдела, выбранного в качестве родительского, не существует!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { try { using (var db = new TestDBEntities()) { var departments = db.Department.ToList(); if (_departmentId == null) { var department = new Department { ID = Guid.Parse(MaskedTextBox_ID.Text), Code = TextBox_CodeName.Text, Name = TextBox_Name.Text }; if (ComboBox_ParentDepartment.SelectedIndex > 0) { department.ParentDepartmentID = departments[ComboBox_ParentDepartment.SelectedIndex - 1].ID; } db.Department.Add(department); } else { var department = db.Department.First(x => x.ID == _departmentId); department.ID = Guid.Parse(MaskedTextBox_ID.Text); department.Code = TextBox_CodeName.Text; department.Name = TextBox_Name.Text; if (ComboBox_ParentDepartment.SelectedIndex > 0) { department.ParentDepartmentID = departments[ComboBox_ParentDepartment.SelectedIndex - 1].ID; } else { department.ParentDepartmentID = null; } } db.SaveChanges(); } this.Close(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { GlobalVars.Log.Error(ex, "DepartmentInfoEditForm: Ошибка при сохранении информации об отделе."); MessageBox.Show($"Возникла ошибка. Возможно некоторые поля заполнены неправильно.\n\n{ex.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }