private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (edited.Value) { if (DialogResult.Yes == MessageBox.Show("Сохранить изменения в БД?", "Внимание", MessageBoxButtons.YesNo)) { try { int kt = 0; int kol = dataSet1.postavki.Count; DataRow[] rows = new DataRow[kol]; foreach (DataRow r in dataSet1.postavki) { if (r.RowState == DataRowState.Deleted) { rows[kt] = r; kt++; } } for (int k = 0; k <= kt; k++) { postavkiTableAdapter.Update(rows[k]); } postavshikiTableAdapter.Update(dataSet1.postavshiki); produktTableAdapter.Update(dataSet1.produkt); kol = dataSet1.postavki.Count; rows = new DataRow[kol]; kt = 0; foreach (DataRow r in dataSet1.postavki) { if (r.RowState == DataRowState.Modified || r.RowState == DataRowState.Added) { rows[kt] = r; kt++; } } for (int k = 0; k <= kt; k++) { postavkiTableAdapter.Update(rows[k]); } dataSet1.AcceptChanges(); edited.Value = false; } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { dataSet1.RejectChanges(); edited.Value = false; } if (DialogResult.No == MessageBox.Show("Выйти?", "Выход", MessageBoxButtons.YesNo)) { e.Cancel = true; } } }
private void btnSave_Click(object sender, RoutedEventArgs e) { if (action == ActionState.New) { try { DataRow newRow = phoneNumbersDataSet.PhoneNumbers.NewRow(); newRow.BeginEdit(); newRow["Phonenum"] = txtPhoneNumber.Text.Trim(); newRow["Subscriber"] = txtSubscriber.Text.Trim(); newRow.EndEdit(); phoneNumbersDataSet.PhoneNumbers.Rows.Add(newRow); tblPhoneNumbersAdapter.Update(phoneNumbersDataSet.PhoneNumbers); phoneNumbersDataSet.AcceptChanges(); } catch (DataException ex) { phoneNumbersDataSet.RejectChanges(); MessageBox.Show(ex.Message); } btnNew.IsEnabled = true; btnEdit.IsEnabled = true; btnSave.IsEnabled = false; btnCancel.IsEnabled = false; lstPhones.IsEnabled = true; btnPrevious.IsEnabled = true; btnNext.IsEnabled = true; txtPhoneNumber.IsEnabled = false; txtSubscriber.IsEnabled = false; } else if (action == ActionState.Edit) { try { DataRow editRow = phoneNumbersDataSet.PhoneNumbers.Rows[lstPhones.SelectedIndex]; editRow.BeginEdit(); editRow["Phonenum"] = txtPhoneNumber.Text.Trim(); editRow["Subscriber"] = txtSubscriber.Text.Trim(); editRow.EndEdit(); tblPhoneNumbersAdapter.Update(phoneNumbersDataSet.PhoneNumbers); phoneNumbersDataSet.AcceptChanges(); } catch (DataException ex) { phoneNumbersDataSet.RejectChanges(); MessageBox.Show(ex.Message); } btnNew.IsEnabled = true; btnEdit.IsEnabled = true; btnDelete.IsEnabled = true; btnSave.IsEnabled = false; btnCancel.IsEnabled = false; lstPhones.IsEnabled = true; btnPrevious.IsEnabled = true; btnNext.IsEnabled = true; txtPhoneNumber.IsEnabled = false; txtSubscriber.IsEnabled = false; txtPhoneNumber.SetBinding(TextBox.TextProperty, txtPhoneNumberBinding); txtSubscriber.SetBinding(TextBox.TextProperty, txtSubscriberBinding); } else if (action == ActionState.Delete) { try { DataRow deleterow = phoneNumbersDataSet.PhoneNumbers.Rows[lstPhones.SelectedIndex]; deleterow.Delete(); tblPhoneNumbersAdapter.Update(phoneNumbersDataSet.PhoneNumbers); phoneNumbersDataSet.AcceptChanges(); } catch (DataException ex) { phoneNumbersDataSet.RejectChanges(); MessageBox.Show(ex.Message); MessageBox.Show(ex.Message); } btnNew.IsEnabled = true; btnEdit.IsEnabled = true; btnDelete.IsEnabled = true; btnSave.IsEnabled = false; btnCancel.IsEnabled = false; lstPhones.IsEnabled = true; btnPrevious.IsEnabled = true; btnNext.IsEnabled = true; txtPhoneNumber.IsEnabled = false; txtSubscriber.IsEnabled = false; txtPhoneNumber.SetBinding(TextBox.TextProperty, txtPhoneNumberBinding); txtSubscriber.SetBinding(TextBox.TextProperty, txtSubscriberBinding); } }