Esempio n. 1
0
        //on selection change, updates dgvProducts to show corresponding products for this supplier
        private void dgvSuppliers_SelectionChanged(object sender, EventArgs e)
        {
            Supplier selectedSupplier = null;

            try
            {
                foreach (DataGridViewRow row in dgvSuppliers.SelectedRows)
                {
                    selectedSupplier = new Supplier(Convert.ToInt32(row.Cells[0].Value.ToString()),
                                                    row.Cells[1].Value.ToString());
                }
                if (productSuppliers == null)
                {
                    productSuppliers = SupplierDB.GetProductSuppliersBySupplier(selectedSupplier);
                    UpdateBinding();
                    frmProduct.ProductListFormat(dgvProducts);
                }
                else
                {
                    productSuppliers = SupplierDB.GetProductSuppliersBySupplier(selectedSupplier);
                    UpdateBinding();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
            }
        }
        private void btnAddToList_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridViewRow row in dgvProducts.SelectedRows)//get each row from dgvProducts
                {
                    selectedProduct = new Product(Convert.ToInt32(row.Cells[0].Value.ToString()),
                                                  row.Cells[1].Value.ToString());                                  //get value from row and assign it to  selectedProduct
                    productSupplier = new ProductSupplier(selectedProduct.ProductId, selectedSupplier.SupplierId); //get value from row and assign it to  productSupplier
                    ProductSupplierDB.InsertProductSupplier(productSupplier);                                      //function to call InsertProductSupplier
                    editedProductSuppliers = SupplierDB.GetProductSuppliersBySupplier(selectedSupplier);           //function to call GetProductSuppliersBySupplier
                    products = ProductDB.GetProductsNotInList(selectedSupplier);                                   //function to call GetProductsNotInList
                }

                UpdateBinding();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());//show exception if there any
            }
            // only select the last row
            dgvProductSuppliers.ClearSelection();
            int index = editedProductSuppliers.Count - 1;

            dgvProductSuppliers.Rows[index].Selected = true;
            txtPName.Clear();
        }
 private void btnRemove_Click(object sender, EventArgs e)
 {
     try
     {
         foreach (DataGridViewRow row in dgvProductSuppliers.SelectedRows)//get each row from dgvProductSuppliers
         {
             selectedProduct = new Product(Convert.ToInt32(row.Cells[0].Value.ToString()),
                                           row.Cells[1].Value.ToString());                                  //get value from row and assign it to  selectedProduct
             productSupplier = new ProductSupplier(selectedProduct.ProductId, selectedSupplier.SupplierId); //get value from row and assign it to  ProductSupplier
             ProductSupplierDB.DeleteProductSupplier(productSupplier);                                      //function to call DeleteProductSupplier
             editedProductSuppliers = SupplierDB.GetProductSuppliersBySupplier(selectedSupplier);           //function to call GetProductSuppliersBySupplier
             products = ProductDB.GetProductsNotInList(selectedSupplier);                                   //function to call GetProductsNotInList
         }
         UpdateBinding();
         dgvProductSuppliers.ClearSelection();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Deleting this product will cause you to lose a record in your booking history. \nThe delete action has been suspended. \nPlease contact DBA.");//show exception if there any
     }
 }