private void btnSubmit_Click(object sender, EventArgs e) { //If user is adding a record, customize message box text accordingly. if (isAddAndNotModify == true) { if (PSDB.addToPSThenConfirmSuccess(cmbProduct.SelectedItem.ToString(), cmbSupplier.SelectedItem.ToString()) == true) { MessageBox.Show("You have added \n\n" + cmbProduct.SelectedItem.ToString() + " provided by " + cmbSupplier.SelectedItem.ToString() + ".", "Success!"); } else { MessageBox.Show("Database error, please contact your administrator"); } DialogResult = DialogResult.OK; } //Otherwise, user is modifying a record. else { //Check how many records in Packages_Products_Suppliers also contain this combination //of product and supplier. //If none, simply ask if user wishes to delete. if (PPSDB.GetPPSWithPS(originalProduct, originalSupplier).Count == 0) { if (PSDB.UpdatePSThenConfirmSuccess(mainForm.currentlyDGVSelectedProductName, mainForm.currentlyDGVSelectedSupplierName, cmbProduct.SelectedItem.ToString(), cmbSupplier.SelectedItem.ToString()) == true) { MessageBox.Show(mainForm.currentlyDGVSelectedProductName + " provided by " + mainForm.currentlyDGVSelectedSupplierName + "\n\nhas been successfully updated to \n\n" + cmbProduct.SelectedItem.ToString() + " provided by " + cmbSupplier.SelectedItem.ToString() + ".", "Success!"); } else { MessageBox.Show("Database error, please contact your administrator"); } DialogResult = DialogResult.OK; } else { frmPSDeleteUpdate secondForm = new frmPSDeleteUpdate(); secondForm.mainFormPSAM = this; secondForm.isDeleteAndNotUpdate = false; DialogResult result = secondForm.ShowDialog(); if (result == DialogResult.OK) { if (PSDB.addToPSThenConfirmSuccess(cmbProduct.SelectedItem.ToString(), cmbSupplier.SelectedItem.ToString()) != true) { MessageBox.Show("Database error, please contact your administrator"); } else { if (PPSDB.UpdatePPSWithPSThenConfirmSuccess(originalProduct, originalSupplier, newlyPickedProduct, newlyPickedSupplier) != true) { MessageBox.Show("Someone has deleted or changed that product(s) in the package(s).", "Concurrency error"); } else { if (PSDB.DeletePSThenConfirm(mainForm.currentlyDGVSelectedProductName, mainForm.currentlyDGVSelectedSupplierName) != true) { MessageBox.Show("Database error, please contact your administrator"); } else { MessageBox.Show(mainForm.currentlyDGVSelectedProductName + " provided by " + mainForm.currentlyDGVSelectedSupplierName + "\n\nhas been successfully updated to \n\n" + cmbProduct.SelectedItem.ToString() + " provided by " + cmbSupplier.SelectedItem.ToString() + ".", "Success!"); } } } DialogResult = DialogResult.OK; } } } }
/// <summary> /// Some cells in the products DGV contain buttons. /// This method contains method for the different buttons. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvPS_CellClick(object sender, DataGridViewCellEventArgs e) { var senderGrid = (DataGridView)sender; //If user clicks a button next to record in column index 0, which is the Delete button, //get ready to delete it. if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0 && e.ColumnIndex == 0) { //Select record and confirm with user to delete it. dgvPS.Rows[e.RowIndex].Selected = true; currentlyDGVSelectedProductName = dgvPS.SelectedCells[2].Value.ToString(); currentlyDGVSelectedSupplierName = dgvPS.SelectedCells[3].Value.ToString(); //Check how many records in Packages_Products_Suppliers also contain this combination //of product and supplier. //If none, simply ask if user wishes to delete. if (PPSDB.GetPPSWithPS(dgvPS.SelectedCells[2].Value.ToString(), dgvPS.SelectedCells[3].Value.ToString()).Count == 0) { DialogResult dialogresult = MessageBox.Show("Are you sure you wish to delete \n\n" + dgvPS.SelectedCells[2].Value.ToString() + " provided by " + dgvPS.SelectedCells[3].Value.ToString() + "?", "Confirm delete", MessageBoxButtons.YesNo); //If user says yes, try to delete record. if (dialogresult == DialogResult.Yes) { if (PSDB.DeletePSThenConfirm(dgvPS.SelectedCells[2].Value.ToString(), dgvPS.SelectedCells[3].Value.ToString()) != true) { MessageBox.Show("Someone has deleted or changed that record in the database. Click OK to refresh the data displayed here.", "Concurrency error"); dgvPS.DataSource = PSDB.GetPS(productFilterCondition, supplierFilterCondition); } else { MessageBox.Show("You have deleted \n\n" + dgvPS.SelectedCells[2].Value.ToString() + " provided by " + dgvPS.SelectedCells[3].Value.ToString() + ".", "Success!"); if (PSDB.GetPS(productFilterCondition, supplierFilterCondition).Count > 0) { dgvPS.DataSource = PSDB.GetPS(productFilterCondition, supplierFilterCondition); } else { dgvPS.DataSource = PSDB.GetPS("", ""); } } } } //If some packages already contain this product and supplier, //inform user that deleting this product and supplier will also //delete it from those packages. Confirm if user wishes to proceed. else { frmPSDeleteUpdate secondForm = new frmPSDeleteUpdate(); secondForm.mainFormPS = this; secondForm.isDeleteAndNotUpdate = true; DialogResult result = secondForm.ShowDialog(); if (result == DialogResult.OK) { if (PPSDB.DeletePPSWithPSThenConfirm(dgvPS.SelectedCells[2].Value.ToString(), dgvPS.SelectedCells[3].Value.ToString()) != true) { MessageBox.Show("Someone has deleted or changed that product(s) in the package(s).", "Concurrency error"); } if (PSDB.DeletePSThenConfirm(dgvPS.SelectedCells[2].Value.ToString(), dgvPS.SelectedCells[3].Value.ToString()) != true) { MessageBox.Show("Someone has deleted or changed that record in the database. Click OK to refresh the data displayed here.", "Concurrency error"); dgvPS.DataSource = PSDB.GetPS(productFilterCondition, supplierFilterCondition); } else { MessageBox.Show("You have deleted \n\n" + dgvPS.SelectedCells[2].Value.ToString() + " provided by " + dgvPS.SelectedCells[3].Value.ToString() + ".", "Success!"); if (PSDB.GetPS(productFilterCondition, supplierFilterCondition).Count > 0) { dgvPS.DataSource = PSDB.GetPS(productFilterCondition, supplierFilterCondition); } else { dgvPS.DataSource = PSDB.GetPS("", ""); } } } } } //If user clicks a button next to record in column index 1, which is the Update button, //update the global variables for the Add/Modify form dialog to access, //then create Add/Modify form dialog. else if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0 && e.ColumnIndex == 1) { dgvPS.Rows[e.RowIndex].Selected = true; currentlyDGVSelectedProductName = dgvPS.SelectedCells[2].Value.ToString(); currentlyDGVSelectedSupplierName = dgvPS.SelectedCells[3].Value.ToString(); frmPSAddModify secondForm = new frmPSAddModify(); secondForm.mainForm = this; DialogResult result = secondForm.ShowDialog(); //If update was successful, update products DGV. if (result == DialogResult.OK) { dgvPS.DataSource = PSDB.GetPS(productFilterCondition, supplierFilterCondition); } } }