private void frmPackages_Load(object sender, EventArgs e)
 {
     packageDataGridView.DataSource = (from p in dbContext.Packages
                                       orderby p.PackageId
                                       select p).ToList();
     dgvPPS.DataSource = PPSDB.GetPPSWithPackageId(Convert.ToInt32(packageDataGridView[0, 0].Value));
 }
        /// <summary>
        /// Do not allow user to add/update if:
        /// - user has not picked a product
        /// - user has not picked a supplier
        /// - the product/supplier the user picked already exists in this package.
        /// </summary>
        private void isReadyForSubmission()
        {
            if (cmbProduct.SelectedItem == null || cmbSupplier.SelectedItem == null)
            {
                btnSubmit.Enabled  = false;
                lblGandalf.Visible = false;
            }
            else if (cmbProduct.SelectedItem.ToString() == "" || cmbSupplier.SelectedItem.ToString() == "")
            {
                btnSubmit.Enabled  = false;
                lblGandalf.Visible = false;
            }
            else if (cmbSupplier.SelectedItem.ToString() == "----------")
            {
                btnSubmit.Enabled  = false;
                lblGandalf.Visible = false;
            }

            //Check if user's selection already exists in this package.
            //If so, prevent submission and display message.
            else if (PPSDB.recordAlreadyExistsInPPS(mainPackageForm.currentlySelectedPackageId,
                                                    cmbProduct.SelectedItem.ToString(), cmbSupplier.SelectedItem.ToString()) == true)
            {
                btnSubmit.Enabled  = false;
                lblGandalf.Visible = true;
            }
            else
            {
                btnSubmit.Enabled  = true;
                lblGandalf.Visible = false;
            }
        }
 /// <summary>
 /// Whenever user selects a different package in the Packages DGV,
 /// the products DGV should show products for that DGV.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void packageDataGridView_SelectionChanged(object sender, EventArgs e)
 {
     //If a row is selected, then refresh data.
     if (packageDataGridView.SelectedCells.Count != 0)
     {
         dgvPPS.DataSource = PPSDB.GetPPSWithPackageId(Convert.ToInt32(packageDataGridView[0, packageDataGridView.SelectedCells[0].RowIndex].Value));
     }
 }
        //After this point, all code written by Ricky.

        /// <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 dataGridView1_CellContentClick(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.
                dgvPPS.Rows[e.RowIndex].Selected = true;
                DialogResult dialogresult = MessageBox.Show("Are you sure you wish to delete \n\n" + dgvPPS.SelectedCells[2].Value.ToString() + " provided by " + dgvPPS.SelectedCells[3].Value.ToString() + "\n\nfrom the package " + packageDataGridView.SelectedCells[1].Value.ToString() + "?", "Confirm delete", MessageBoxButtons.YesNo);

                //If user says yes, try to delete record.
                if (dialogresult == DialogResult.Yes)
                {
                    if (PPSDB.DeletePPSWithPackageIdThenConfirm(Convert.ToInt32(packageDataGridView.SelectedCells[0].Value), dgvPPS.SelectedCells[2].Value.ToString(), dgvPPS.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");
                        dgvPPS.DataSource = PPSDB.GetPPSWithPackageId(Convert.ToInt32(packageDataGridView[0, packageDataGridView.SelectedCells[0].RowIndex].Value));
                    }
                    else
                    {
                        MessageBox.Show("You have deleted \n\n" + dgvPPS.SelectedCells[2].Value.ToString() + " provided by " + dgvPPS.SelectedCells[3].Value.ToString() + "\n\nfrom the package " + packageDataGridView.SelectedCells[1].Value.ToString() + ".", "Success!");
                        dgvPPS.DataSource = PPSDB.GetPPSWithPackageId(Convert.ToInt32(packageDataGridView[0, packageDataGridView.SelectedCells[0].RowIndex].Value));
                    }
                }
            }

            //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)
            {
                dgvPPS.Rows[e.RowIndex].Selected = true;
                currentlySelectedPackageId       = Convert.ToInt32(packageDataGridView.SelectedCells[0].Value);
                currentlySelectedPackageName     = packageDataGridView.SelectedCells[1].Value.ToString();
                currentlySelectedProductName     = dgvPPS.SelectedCells[2].Value.ToString();
                currentlySelectedSupplierName    = dgvPPS.SelectedCells[3].Value.ToString();
                frmPPSAddModify secondForm = new frmPPSAddModify();
                secondForm.mainPackageForm = this;
                DialogResult result = secondForm.ShowDialog();

                //If update was successful, update products DGV.
                if (result == DialogResult.OK)
                {
                    dgvPPS.DataSource = PPSDB.GetPPSWithPackageId(Convert.ToInt32(packageDataGridView[0, packageDataGridView.SelectedCells[0].RowIndex].Value));
                }
            }
        }
        /// <summary>
        /// When user clicks 'Add product to package' button, update
        /// the global variables for the Add/Modify form dialog to access,
        /// then create Add/Modify form dialog.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddPPS_Click(object sender, EventArgs e)
        {
            currentlySelectedPackageId    = Convert.ToInt32(packageDataGridView.SelectedCells[0].Value);
            currentlySelectedPackageName  = packageDataGridView.SelectedCells[1].Value.ToString();
            currentlySelectedProductName  = "";
            currentlySelectedSupplierName = "";
            frmPPSAddModify secondForm = new frmPPSAddModify();

            secondForm.mainPackageForm = this;
            DialogResult result = secondForm.ShowDialog();

            //If a product was succesfully added, update products DGV.
            if (result == DialogResult.OK)
            {
                dgvPPS.DataSource = PPSDB.GetPPSWithPackageId(Convert.ToInt32(packageDataGridView[0, packageDataGridView.SelectedCells[0].RowIndex].Value));
            }
        }
 /// <summary>
 /// Load data and form appearance depending on whether we are deleting or updating.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void frmPSDelete_Load(object sender, EventArgs e)
 {
     if (isDeleteAndNotUpdate == true)
     {
         lblWarning.Text   = mainFormPS.currentlyDGVSelectedProductName + " provided by " + mainFormPS.currentlyDGVSelectedSupplierName + "\n\nwill also be removed from the following packages:";
         dgvPPS.DataSource = PPSDB.GetPPSWithPS(mainFormPS.currentlyDGVSelectedProductName, mainFormPS.currentlyDGVSelectedSupplierName);
         lblDirection.Text = "Click 'Delete from this table AND all packages' to do so; otherwise click 'Cancel.";
         btnOK.Text        = "Delete from this table AND all packages";
     }
     else
     {
         lblWarning.Text   = mainFormPSAM.originalProduct + " provided by " + mainFormPSAM.originalSupplier + "\n\nwill also be updated to " + mainFormPSAM.newlyPickedProduct + " provided by " + mainFormPSAM.newlyPickedSupplier + " in the following packages:";
         dgvPPS.DataSource = PPSDB.GetPPSWithPS(mainFormPSAM.originalProduct, mainFormPSAM.originalSupplier);
         lblDirection.Text = "Click 'Update in this table AND all packages' to do so; otherwise click 'Cancel.";
         btnOK.Text        = "Update in this table AND all packages";
     }
 }
        /// <summary>
        /// Any combination of product and supplier must exist in the Products_Suppliers
        /// table before it can be added to the Packages_Products_Suppliers table.
        /// Therefore, when user clicks Submit button, first check if user's selection exists
        /// in Products_Suppliers
        /// - if yes, then add it to Packages_Products_Suppliers
        /// - if not, add it to Products_Suppliers first, then add to Packages_Products_Suppliers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //bool value determines whether to proceed with add/modify, depending on some
            //if conditions.
            bool proceed = true;

            //Check if user's selection exists in Products_Suppliers, and check ifthey still wish to proceed.
            //The only time where we do NOT proceed is if user clicks 'No' in dialog or if there is database error.
            if (PSDB.recordExistsInPS(cmbProduct.SelectedItem.ToString(), cmbSupplier.SelectedItem.ToString()) == false)
            {
                DialogResult dialogresult = MessageBox.Show("Are you sure that " + cmbProduct.SelectedItem.ToString() + " is being provided by " +
                                                            cmbSupplier.SelectedItem.ToString() + "? \n\nThis information is not in our database.", "Confirm information", MessageBoxButtons.YesNo);
                if (dialogresult == DialogResult.Yes)
                {
                    if (PSDB.addToPSThenConfirmSuccess(cmbProduct.SelectedItem.ToString(), cmbSupplier.SelectedItem.ToString()) != true)
                    {
                        MessageBox.Show("Database error, please contact your administrator");
                        proceed = false;
                    }
                }
                else
                {
                    proceed = false;
                }
            }

            //If we are clear to proceed with add/modify, proceed.
            if (proceed == true)
            {
                //If user is adding a record, customize message box text accordingly.
                if (isAddAndNotModify == true)
                {
                    if (PPSDB.addToPPSThenConfirmSuccess(mainPackageForm.currentlySelectedPackageId, cmbProduct.SelectedItem.ToString(),
                                                         cmbSupplier.SelectedItem.ToString()) == true)
                    {
                        MessageBox.Show("You have added \n\n" + cmbProduct.SelectedItem.ToString() + " provided by " + cmbSupplier.SelectedItem.ToString() +
                                        "\n\nto the package " + mainPackageForm.currentlySelectedPackageName + ".", "Success!");
                    }
                    else
                    {
                        MessageBox.Show("Database error, please contact your administrator");
                    }

                    DialogResult = DialogResult.OK;
                }

                //Otherwise, user is modifying a record, so customize message box text accordingly.
                else
                {
                    if (PPSDB.UpdatePPSThenConfirmSuccess(mainPackageForm.currentlySelectedPackageId, mainPackageForm.currentlySelectedProductName,
                                                          mainPackageForm.currentlySelectedSupplierName, cmbProduct.SelectedItem.ToString(), cmbSupplier.SelectedItem.ToString()) == true)
                    {
                        MessageBox.Show(mainPackageForm.currentlySelectedProductName + " provided by " + mainPackageForm.currentlySelectedSupplierName +
                                        "\n\nin the package " + mainPackageForm.currentlySelectedPackageName + " has 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;
                }
            }
        }
        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;
                    }
                }
            }
        }
Beispiel #9
0
        /// <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);
                }
            }
        }