// метод добавления/редактирования причины списания private void add_edit_reason_debit() { if (String.IsNullOrWhiteSpace(this.textBox_CipherReasonDebit.Text) || String.IsNullOrWhiteSpace(this.textBox_NameReasonDebit.Text)) { MessageBox.Show("Чтобы добавить или изменить запись заполните все поля!", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // если добавляем if (this.EditReasonDebit == null) { ReasonDebit newReasonDebit = new ReasonDebit(); newReasonDebit.code = this.textBox_CipherReasonDebit.Text; newReasonDebit.name = this.textBox_NameReasonDebit.Text; Program.db.ReasonDebits.Add(newReasonDebit); MessageBox.Show("Успешно добавлено!", "Успех!", MessageBoxButtons.OK, MessageBoxIcon.Information); } // редактируем else { this.EditReasonDebit.code = this.textBox_CipherReasonDebit.Text; this.EditReasonDebit.name = this.textBox_NameReasonDebit.Text; Program.db.ReasonDebits.Update(this.EditReasonDebit); MessageBox.Show("Успешно изменено!", "Успех!", MessageBoxButtons.OK, MessageBoxIcon.Information); } Program.db.SaveChanges(); }
/* Для редактирования строки акта */ private void SaveBeforeEdit() { workshopSaved = (Workshop)comboBox_Workshop.SelectedItem; groupSaved = (EquipmentGroup)comboBox_GroupEquipment.SelectedItem; equipmentSaved = (Equipment)comboBox_Equipment.SelectedItem; reasonSaved = (ReasonDebit)comboBox_ReasonDebit.SelectedItem; stateAllSaved = checkBox_AllGroupDebit.Checked; }
public AddReasonDebitForm(ReasonDebit reasonDebit = null) { InitializeComponent(); if (reasonDebit != null) { this.Text = "Справочник - Редактировать причину списания"; this.button_Add.Text = "Изменить"; this.button_AddAndClose.Text = "Изменить и закрыть"; this.textBox_CipherReasonDebit.Text = reasonDebit.code; this.textBox_NameReasonDebit.Text = reasonDebit.name; this.EditReasonDebit = reasonDebit; } }
private void button_EditReasonDebit_Click(object sender, EventArgs e) { int PK_Reason_Debit = -1; try { PK_Reason_Debit = Convert.ToInt32(dataGridView_DataSearch.SelectedRows[0].Cells[0].Value); } catch (Exception exception) { MessageBox.Show(exception.Message); } ReasonDebit reasonDebit = Program.db.ReasonDebits.Find(PK_Reason_Debit); if (reasonDebit != null) { new AddReasonDebitForm(reasonDebit).ShowDialog(); } }
private void button_RemoveReasonDebit_Click(object sender, EventArgs e) { // сформируем список для удаления // заранее - потому что messagebox, который отвечает за подтверждение удаления (ниже) // вызывает событие Activated, что обновляет таблицу // из-за этого selectedrows сбрасывается... List <ReasonDebit> reasonDebitsForRemove = new List <ReasonDebit>(); int PK_Reason_Debit = -1; try { foreach (DataGridViewRow row in dataGridView_DataSearch.SelectedRows) { PK_Reason_Debit = Convert.ToInt32(row.Cells[0].Value); ReasonDebit curReasonDebit = Program.db.ReasonDebits.Find(PK_Reason_Debit); if (curReasonDebit != null) { reasonDebitsForRemove.Add(curReasonDebit); } } } catch (Exception exception) { MessageBox.Show(exception.Message); } // запрос подтверждения if (MessageBox.Show("Вы действительно хотите удалить выбранные причны списания? Данное действие нельзя будет отменить!", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } // Оставим в списке те, которые можно удалить List <ReasonDebit> good_list = new List <ReasonDebit>(); bool check = false; // флаг - имеются записи, которые не будут удалены foreach (var item in reasonDebitsForRemove) { if (item.is_there_any_row() == false) { good_list.Add(item); } else { check = true; } } // запрос подтверждения if (good_list.Count() > 0 && check) { if (MessageBox.Show("Среди выбранных причин списания есть те, которые невозможно удалить, т.к. имеется списанное оборудование с такими причинами. \n\nУдалить причины списания из справочника, которые не вызвали такого конфликта?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } } else if (good_list.Count() == 0 && check) { MessageBox.Show("Невозможно выполнить удаление. Имеется списанное оборудования с выбранными причинами списания!"); } // применим удаление foreach (var reason in good_list) { Program.db.Remove(reason); } Program.db.SaveChanges(); this.button_Search.PerformClick(); }