// Tab Two Changed (by using ALL, EDIT, ADD btn): change UI appearance accordingly private void twoTab_SelectedIndexChanged(object sender, EventArgs e) { twoBtnViewAll.BackColor = Color.DarkCyan;; twoBtnEdit.BackColor = Color.DarkCyan; twoBtnAdd.BackColor = Color.DarkCyan; twoBtnSave.Visible = true; if (twoTab.SelectedIndex == 0) { twoBtnSave.Visible = false; // 'ALL' tab, load all Product Supplier data and fill DataSource _psList = Products_suppliersDB.GetAllProductSupplierWithNames().OrderBy(ps => ps.ProdName).ToList(); // use List to make a SortableBindingList var _sortableList = new SortableBindingList <ProductSupplierWithName>(_psList); productSupplierWithNameBindingSource.DataSource = _sortableList; // databinding for combo boxes suppliersBindingSource.DataSource = SuppliersDB.GetSuppliers().OrderBy(s => s.SupName); productsBindingSource.DataSource = ProductsDB.GetProducts(); } else if (twoTab.SelectedIndex == 2) { // 'ADD' tab // select nothing when load twoCmbAddProdName.SelectedIndex = -1; twoCmbAddSuppName.SelectedIndex = -1; } // 'EDIT' tab }
// dropdown list selected: filter for second dropdown list private void twoCmbAddProdName_SelectedIndexChanged(object sender, EventArgs e) { // if nothing selected, return if (twoCmbAddProdName.SelectedIndex == -1) { return; } // filtering var suppliers = SuppliersDB.GetSuppliers(); var prodSupps = Products_suppliersDB.GetProductsSuppliers(); var _psHasIds = prodSupps.FindAll(ps => ps.ProductId == (int)twoCmbAddProdName.SelectedValue).Select(ps => ps.SupplierId); List <Suppliers> filteredSupp = new List <Suppliers>(); foreach (var supp in suppliers) { if (!_psHasIds.Contains(supp.SupplierId)) { filteredSupp.Add(supp); } } suppliersBindingSource.DataSource = filteredSupp; twoCmbAddSuppName.SelectedIndex = -1; }
// SAVE btn clicked: update or add new private void twoBtnSave_Click(object sender, EventArgs e) { if (twoTab.SelectedIndex == 1) // EDIT MODE { // get the current Product_supplier obj in order to compare with new one var currentPS = Products_suppliersDB.GetProductsSuppliers() .SingleOrDefault(ps => ps.ProductSupplierId == Convert.ToInt32(twoTxtProdSuppId.Text)); // create new Product_supplier obj using user's change var newProdSupp = new Products_suppliers { ProductId = Convert.ToInt32(twoCmbProdName.SelectedValue), SupplierId = Convert.ToInt32(twoCmbSupName.SelectedValue) }; // compare old and new, see if there is any change if (currentPS.ProductId == newProdSupp.ProductId && currentPS.SupplierId == newProdSupp.SupplierId) { // no change, show message MessageBox.Show("No change were found.", "Please make changes"); } else { // have change, try to update database try { var rowsAffected = Products_suppliersDB.UpdateProductSupplier(currentPS, newProdSupp); MessageBox.Show(rowsAffected + " record is successfully updated."); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } else if (twoTab.SelectedIndex == 2) // ADD MODE { // do validation, make sure user select Product and Supplier if (twoCmbAddProdName.SelectedIndex < 0 || twoCmbAddSuppName.SelectedIndex < 0) { MessageBox.Show("Required data missing.", "Please choose Product and Supplier"); twoCmbAddProdName.SelectedIndex = -1; twoCmbAddSuppName.SelectedIndex = -1; return; } // create new Product_supplier obj using user's choices var newProdSupp = new Products_suppliers { ProductId = Convert.ToInt32(twoCmbAddProdName.SelectedValue), SupplierId = Convert.ToInt32(twoCmbAddSuppName.SelectedValue) }; // try to insert new obj try { var newId = Products_suppliersDB.AddProductSupplier(newProdSupp); MessageBox.Show($"Product Supplier was successfully added, new record id: {newId}.", "Congratulations"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }