private void EditCurrentCustomer() { if (_bsCustomers.DataSource == null) { MessageBox.Show("Please select some data"); return; } var contactList = _dataOperations.RetrieveContactTitles(); CustomerEditorForm f = new CustomerEditorForm(); try { f.CompanyNameTextBox.Text = _bsCustomers.CurrentRow().CompanyName(); f.ContactNameTextBox.Text = _bsCustomers.CurrentRow().ContactName(); f.cboContactTitles.DataSource = contactList; f.cboContactTitles.SelectedIndex = contactList.FindIndex((contactType) => contactType.ContactType == _bsCustomers.CurrentRow() .ContactTitle()); if (f.ShowDialog() == DialogResult.OK) { if (f.ValidateTextBoxes()) { var customerIdentifier = _bsCustomers.CurrentRow().Identifier(); var contactTypeIdentifier = ((ContactTypes)f.cboContactTitles.SelectedItem).Identifier; if (_dataOperations.UpdateCustomer(customerIdentifier, f.CompanyNameTextBox.Text, f.ContactNameTextBox.Text, contactTypeIdentifier)) { _bsCustomers.CurrentRow().SetCompanyName(f.CompanyNameTextBox.Text); _bsCustomers.CurrentRow().SetContactName(f.ContactNameTextBox.Text); _bsCustomers.CurrentRow().SetContactTitle(f.cboContactTitles.Text); } } else { MessageBox.Show("Nothing save as one or more fields were empty."); } } } finally { f.Dispose(); } }
/// <summary> /// Insert a new row /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <remarks></remarks> private void NewCustomerButton_Click(object sender, EventArgs e) { if (_bsCustomers.DataSource == null) { MessageBox.Show("Please select some data"); return; } CustomerEditorForm f = new CustomerEditorForm(); try { f.cboContactTitles.DataSource = _dataOperations.RetrieveContactTitles(); if (f.ShowDialog() == DialogResult.OK) { var contactTypeIdentifier = ((ContactTypes)f.cboContactTitles.SelectedItem).Identifier; int primaryKey = _dataOperations.AddCustomer(f.CompanyNameTextBox.Text, f.ContactNameTextBox.Text, contactTypeIdentifier); if (primaryKey != -1) { _bsCustomers.AddRow( primaryKey, f.CompanyNameTextBox.Text, f.ContactNameTextBox.Text, f.cboContactTitles.Text, contactTypeIdentifier); } } } finally { f.Dispose(); } }