// Takes current selected Product_Supplier and adds it to the package [Ronnie] private void btnAdd_Click(object sender, EventArgs e) { // Get ProductSupplierID from combobox int prodSupID = Convert.ToInt32(productSupplierIdTextBox.Text); int packageID = currentPackage.PackageId; // if the current product and supplier combination does not exist for the package, add it to the database if (!TravelExpertsQueryManager.ExistPackagesProductsSupplier(packageID, prodSupID)) { // Create a PackagesProductSupplier with that ID, and the Package ID from the current package Packages_Products_Supplier newPackProdSup = new Packages_Products_Supplier { ProductSupplierId = prodSupID, PackageId = packageID }; // Add that PackagesProductsSupplier to the db using (travelexpertsDataContext dbContext = new travelexpertsDataContext()) { // insert through data context object from the main form dbContext.Packages_Products_Suppliers.InsertOnSubmit(newPackProdSup); dbContext.SubmitChanges(); // submit to the database } // Re-load the datagrid view refreshDataGrid(); } else { MessageBox.Show("The selected product and supplier portfolio has already been added to the package. Please try again.", "Input Error"); } }
private void refreshDataGrid() { // Populate data grid showing products using (travelexpertsDataContext db = new travelexpertsDataContext()) { grdProdSup.DataSource = TravelExpertsQueryManager.FindProdInfoByPackage(db, currentPackage.PackageId); } }
private void DisplayProdSupId() { using (travelexpertsDataContext db = new travelexpertsDataContext()) { int prodId = Convert.ToInt32(prodNameComboBox.SelectedValue); int supId = Convert.ToInt32(supNameComboBox.SelectedValue); productSupplierIdTextBox.Text = TravelExpertsQueryManager.FindProdSuppID(db, prodId, supId).ToString(); } }
// Updates the display of the prodsuppID private void RefreshProdSupId() { using (travelexpertsDataContext db = new travelexpertsDataContext()) { int prodId = Convert.ToInt32(productIdComboBox.SelectedValue); // grabs id from product dropdown int supId = Convert.ToInt32(supplierIdComboBox.SelectedValue); // grabs id from supplier dropdown productSupplierIdTextBox.Text = TravelExpertsQueryManager.FindProdSuppID(db, prodId, supId).ToString(); // uses that data to grab the corresponding prodsup id } }
private void prodNameComboBox_SelectedIndexChanged(object sender, EventArgs e) { // get the list of suppliers applicable with the selected product ID using (travelexpertsDataContext db = new travelexpertsDataContext()) { supNameComboBox.DataSource = TravelExpertsQueryManager.GetSuppliersByProductID(db, Convert.ToInt32(prodNameComboBox.SelectedValue)); DisplayProdSupId(); } }
/// <summary> /// Refreshes data for the associated packages datagrid, based on current selected row in prod_supp grid /// </summary> private void RefreshPackagesByProdSuppGrid() { //// Grab ID of the row currently selected in the Prod_Supp datagrid selectedProdID = Convert.ToInt32(grdProductSuppliers.SelectedRows[0].Cells[0].Value); // Set title for the products data using that current id lblAssociatesPackages.Text = $"Associated Packages for selected product (ID #{selectedProdID})"; // Use a query to get only packages associated with the prod_supp id, and bind it to the grid grdPackagesforProdSupp.DataSource = TravelExpertsQueryManager.GetPackagesByProdSuppID(selectedProdID); }
// Updates the data and display just for the grid of associated products, based on current selection of package datagrid private void RefreshProductGrid() { // Grab ID of the row currently selected in the Packages Datagrid. [Eric] selectedPackageId = Convert.ToInt32(grdPackages.CurrentRow.Cells[0].Value); // Set title for the products data using that current id lblSelectedProdsTitle.Text = $"Products for Selected Package (ID #{selectedPackageId})"; // Populate data in Product Info gridview using (travelexpertsDataContext db = new travelexpertsDataContext()) { // Use an in-depth query to grab the info needed for the product info data grid dataGridView1.DataSource = TravelExpertsQueryManager.FindProdInfoByPackage(db, selectedPackageId); } }
// Initial form setup private void frmProdSupplierAddEdit_Load(object sender, EventArgs e) { // On load, populate data for all data displays rootDB = new travelexpertsDataContext(); // create a new context //get product_supplier data for top datagrid products_SupplierBindingSource.DataSource = TravelExpertsQueryManager.GetProductsSuppliersExtended(rootDB); selectedProdID = Convert.ToInt32(grdProductSuppliers.Rows[0].Cells[0].Value); // set selectedProdID as ID of top row lblSelectedProdsTitle.Text = $"Modify details for selected product (ID #{selectedProdID})"; // Set display for that ID supplierIdComboBox.DataSource = rootDB.Suppliers; // get supplier data for suppliers details dropbox productIdComboBox.DataSource = rootDB.Products; // get product data for products details dropbox RefreshPackagesByProdSuppGrid(); // get package data for selected product_supplier row (in this case, top one) SetDataGridToFirstEntry(); // Selects the first row of the main datagrid }
//If Modify Button was clicked on Form1 private void frmAddModify_Load(object sender, EventArgs e) { if (!isAdd) // Set up for Modify mode - use the Package passed from the last form to populate fields { // Update the title and description of the page lblTitle.Text = "Package Manager - Edit Package"; lblDesc.Text = $"Edit any details and modify product list for the current package (ID #{currentPackage.PackageId})."; using (travelexpertsDataContext db = new travelexpertsDataContext()) { // Grab current package ID used to create this modify page int packageId = currentPackage.PackageId; // Use an in-depth query to grab the info needed for the product info data grid dataGridView1.DataSource = TravelExpertsQueryManager.FindProdInfoByPackage(db, currentPackage.PackageId); } // Set up a snapshot of current associated package_product_suppliers entries ppsSnapshot = TravelExpertsQueryManager.GetPackagesProductsSuppliersByPackageID(currentPackage.PackageId); // handle nullable datetime if (currentPackage.PkgStartDate == null) { EmptyDateTimePicker(pkgStartDateDateTimePicker); } if (currentPackage.PkgEndDate == null) { EmptyDateTimePicker(pkgEndDateDateTimePicker); } // Display current package information in details view packageBindingSource.Add(currentPackage); } else // Set up for Add mode { // Update the title and description of the page lblTitle.Text = "Package Manager - Add A New Package"; lblDesc.Text = "Add details and products for a new package."; EmptyDateTimePicker(pkgStartDateDateTimePicker); EmptyDateTimePicker(pkgEndDateDateTimePicker); } }
//To add Products to a Package - calls form ProdSuppliers private void btnEditAddProducts_Click(object sender, EventArgs e) { frmProdSupplier prodsForm = new frmProdSupplier(); prodsForm.currentPackage = currentPackage; DialogResult result = prodsForm.ShowDialog(); // display second form modal if (result == DialogResult.OK) // new row got inserted { // Toggle a switch noting that products were updated - checked in the event of cancel didAddProducts = true; // Show the updated list of products associated with this package using (travelexpertsDataContext db = new travelexpertsDataContext()) { dataGridView1.DataSource = TravelExpertsQueryManager.FindProdInfoByPackage(db, currentPackage.PackageId); } } }
// Checking off the top filter button will toggle filtering all products in the product_supplier datagrid (either all of them, or just those with packages) private void checkboxFilterProducts_CheckedChanged(object sender, EventArgs e) { // Update the persisting db image (used as a data source) rootDB = new travelexpertsDataContext(); // If the user checks off the option to filter if (checkboxFilterProducts.Checked == true) { // Query PPS to find all ProductSuppliers associated to packages List <int> prodSupIDsWithPackages = rootDB.Packages_Products_Suppliers.Select(pps => pps.ProductSupplierId) // just need ids .ToList(); // Filter the datagrid to only show those entries products_SupplierBindingSource.DataSource = TravelExpertsQueryManager.GetProductsSuppliersExtended(rootDB, prodSupIDsWithPackages); } else { products_SupplierBindingSource.DataSource = TravelExpertsQueryManager.GetProductsSuppliersExtended(rootDB); } }
// Whenever the user selects a product from the dropdown, update the suppliers dropdown with appropriate data [Eric] private void productIdComboBox_SelectedIndexChanged(object sender, EventArgs e) { using (travelexpertsDataContext db = new travelexpertsDataContext()) { // If the user has selected the option to filter suppliers if (checkBoxFilterSuppliers.Checked == true) { // Get the list of suppliers applicable with the selected product ID supplierIdComboBox.DataSource = TravelExpertsQueryManager.GetSuppliersByProductID(db, Convert.ToInt32(productIdComboBox.SelectedValue)); // Make top option of suppliers dropdown selected supplierIdComboBox.SelectedIndex = 0; } else { supplierIdComboBox.DataSource = db.Suppliers; // get supplier data for suppliers details dropbox // Make top option of suppliers dropdown selected supplierIdComboBox.SelectedIndex = 0; } } }
// Go back to last page without saving changes. This may involve some cleaning up, depending on the mode and if products were added [Eric] private void btnCancel_Click(object sender, EventArgs e) { using (travelexpertsDataContext dbContext = new travelexpertsDataContext()) { // First, we have to check to see if any products were added to the package before cancelling if (didAddProducts) // this will be true if so - no need to spend time querying the db { // Get the current PPS entries in the database corresponsind to this package List <Packages_Products_Supplier> ppsCurrent = TravelExpertsQueryManager.GetPackagesProductsSuppliersByPackageID(currentPackage.PackageId); // Next, we have to get the PPS entries to re-add (if they were deleted) and/or remove (if new ones were added) // This is not a super efficient process, but packages shouldn't have enough products for it to make much difference // First, get the ones to re-add List <Packages_Products_Supplier> ppsToAdd = ppsSnapshot // Creating a list of Package_Product_Suppliers where... .Where(ppsSnap => !ppsCurrent // ...for each snapshot entry, it is NOT the case that... .Any(ppsCurr => ppsCurr.ProductSupplierId == ppsSnap.ProductSupplierId)) // ...any current entry has that snapshot entry's ProductSupplierID .ToList(); // Next, the ones to remove List <Packages_Products_Supplier> ppsToDelete = ppsCurrent // Creating a list of Package_Product_Suppliers where... .Where(ppsCurr => !ppsSnapshot // ...for each current entry, it is NOT the case that... .Any(ppsSnap => ppsCurr.ProductSupplierId == ppsSnap.ProductSupplierId)) // ...any snapshot entry has that current entry's ProductSupplierID .ToList(); // Add the needed entries back foreach (Packages_Products_Supplier ppsA in ppsToAdd) { // LINQ to SQL doesn't let you re-add old entity objects, so we need to create copies to add back in place Packages_Products_Supplier clone = new Packages_Products_Supplier { PackageId = ppsA.PackageId, ProductSupplierId = ppsA.ProductSupplierId }; dbContext.Packages_Products_Suppliers.InsertOnSubmit(clone); } // Delete the entries to undo foreach (Packages_Products_Supplier ppsD in ppsToDelete) { // Deleting only works on entities from the current context, so need to grab them // I'm sure this could be done in the ones-to-remove LINQ query above, but I couldn't manage it Packages_Products_Supplier deleteTarget = dbContext.Packages_Products_Suppliers // Search in the table... .Single(pps => // ...for the one entry, with... pps.ProductSupplierId == ppsD.ProductSupplierId && //...the matching ProductSupplierID... pps.PackageId == ppsD.PackageId); //... and the matching PackageID dbContext.Packages_Products_Suppliers.DeleteOnSubmit(deleteTarget); } // Save changes. Phew! dbContext.SubmitChanges(); } // One more step if in Add mode. // A package was inserted into the database initially (to get the add products half to work). // So, if cancelling, we want to delete it this created package. if (isAdd) { // Delete package Package packToDelete = dbContext.Packages .Single(p => p.PackageId == currentPackage.PackageId); dbContext.Packages.DeleteOnSubmit(packToDelete); dbContext.SubmitChanges(); // submit to the database } } // Exit the form DialogResult = DialogResult.Cancel; this.Close(); }