// ------------Added by Wei Guang Yan---------------- // method of Updating suplier's name to database public void UpdateSupplier(string txtUpdate) { // Initialization for messagebox string message; string title = "Supplier Update"; MessageBoxButtons button = MessageBoxButtons.OK; MessageBoxIcon icon = MessageBoxIcon.Warning; // Excute updating int id = Convert.ToInt32(comboBox2.SelectedValue); bool flag = SuppliersDB.UpdateSupplier(id, txtUpdate); // Check whether updating is successful, and show message if (flag == true) { int index = comboBox2.SelectedIndex; comboBox2.DataSource = SuppliersDB.GetSuppliers(); comboBox2.DisplayMember = "SupName"; comboBox2.ValueMember = "SupplierId"; comboBox2.SelectedIndex = index; message = "The supplier is updated successfully!"; icon = MessageBoxIcon.None; } else { message = "Error: Fail to update supplier!\n Please try again or contact IT support."; icon = MessageBoxIcon.Error; } MessageBox.Show(message, title, button, icon); }
// ------------Added by Wei Guang Yan---------------- // Method of inserting suplier's name to database public void AddSupplier(string txtAdd) { // Innitialize for messagebox string message; string title = "New Product Insert"; MessageBoxButtons button = MessageBoxButtons.OK; MessageBoxIcon icon; // Generate new supplier Id based on max id in database int id = SuppliersDB.GetMaxId() + 1; // Add new item to database bool flag = SuppliersDB.AddSupplier(id, txtAdd); // Check whether adding excution is successful, and show message if (flag == true) { comboBox2.DataSource = SuppliersDB.GetSuppliers(); comboBox2.DisplayMember = "SupName"; comboBox2.ValueMember = "SupplierId"; comboBox2.SelectedIndex = comboBox2.Items.Count - 1; txtAddItem.Text = ""; message = "The supplier is added successfully!"; icon = MessageBoxIcon.None; } else { message = "Error: Fail to add new supplier!\n Please try again or contact IT supports."; icon = MessageBoxIcon.Error; } MessageBox.Show(message, title, button, icon); }
//update the supplier list when new product selected private void lstProducts_SelectedIndexChanged_1(object sender, EventArgs e) { currentProduct = ProductDB.getProductById(Convert.ToInt32(lstProducts.SelectedValue)); supplierList = SuppliersDB.GetSuppliers(); //concurrency issue occurs if product is deleted from DB LoadProductSuppliers(currentProduct.ProductId); }
// 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 }
/// <summary> /// Load supplier tab data /// </summary> Raymond private void loadSupplierTab() { supplierCount = 0; suppliersDetails = SuppliersDB.GetSuppliers(); supplierId.Text = suppliersDetails[supplierCount].SupplierId.ToString(); supplierName.Text = ProcessNullSupplierName(suppliersDetails[supplierCount].SupName); }
/// <summary> /// Update All List of Infos /// </summary> private void UpdateAllInfos() { AllPackages = PackagesDB.GetPackages(); AllProducts = ProductsDB.GetProducts(); AllSuppliers = SuppliersDB.GetSuppliers(); ProSupLinkages = ProSupDB.GetProSups(); PacProSupLinkages = PacProSupDB.GetPacProSup(); }
//resets the form (useful to update the datagrid for changes/new submissions) private void clearContent() { supIDTextBox.Clear(); supNameTextBox.Clear(); //resets the datagrid suppliers = SuppliersDB.GetSuppliers(); supplierDataGridView.DataSource = suppliers; }
// Edit existing supplier - Raymond private void btnEdit_Click_1(object sender, EventArgs e) { frmEditSuppliers editSuppliers = new frmEditSuppliers(suppliersDetails[supplierCount]); DialogResult result = editSuppliers.ShowDialog(); suppliersDetails = SuppliersDB.GetSuppliers(); supplierId.Text = suppliersDetails[supplierCount].SupplierId.ToString(); supplierName.Text = ProcessNullSupplierName(suppliersDetails[supplierCount].SupName); }
// Convenient Method: find if there is a duplicated supplier name in DB, return bool private bool FindDuplicatedSupplierName(string supplierName) { if (SuppliersDB.GetSuppliers().Find(s => s.SupName == supplierName) == null) { return(false); } else { MessageBox.Show("The supplier you input already exists.", "Duplicated Name"); return(true); } }
private void btnSave_Click(object sender, EventArgs e) { frmModifySup addSupplierForm = new frmModifySup(); addSupplierForm.addSuppliers = true; DialogResult result = addSupplierForm.ShowDialog(); if (result == DialogResult.OK) { dataGridView1.DataSource = SuppliersDB.GetSuppliers(); } }
//Adding a new supplier - Raymond private void btnNew_Click(object sender, EventArgs e) { frmAddSupplier addSupplier = new frmAddSupplier(); DialogResult result = addSupplier.ShowDialog(); suppliersDetails = SuppliersDB.GetSuppliers(); if (result == DialogResult.OK) { supplierCount = suppliersDetails.Count - 1; } supplierId.Text = suppliersDetails[supplierCount].SupplierId.ToString(); supplierName.Text = ProcessNullSupplierName(suppliersDetails[supplierCount].SupName); }
/********************************************************** * * Form set-up methods * * * ********************************************************/ //get products from database and load the products in the list box private void LoadProducts(int selectedProductId = -1) { //get the product and supplier lists from database productsList = ProductDB.GetProducts(); supplierList = SuppliersDB.GetSuppliers(); //populate product list box lstProducts.DataSource = productsList; //no currentProduct object exists, create one by selecting first item in the lstProducts if (selectedProductId == -1) { lstProducts.SelectedIndex = 0; currentProduct = ProductDB.getProductById(Convert.ToInt32(lstProducts.SelectedValue)); } }
// ----- 4TH NAV BTN: Suppliers ----- // Tab Four Changed (by using ALL, EDIT, ADD btn): change UI appearance accordingly private void fourTab_SelectedIndexChanged(object sender, EventArgs e) { fourBtnSave.Visible = true; fourBtnAll.BackColor = Color.DarkCyan; fourBtnEdit.BackColor = Color.DarkCyan; fourBtnAdd.BackColor = Color.DarkCyan; if (fourTab.SelectedIndex == 0) { // view ALL mode: hide save btn fourBtnSave.Visible = false; // bind data to gridview's binding source var suppliersList = SuppliersDB.GetSuppliers(); var _sortableSuppliers = new SortableBindingList <Suppliers>(suppliersList); suppliersBindingSource.DataSource = _sortableSuppliers; } }
// display suppliers after getting list by DB method private void DisplaySuppliers() { suppliersList = SuppliersDB.GetSuppliers(); if (suppliersList != null) // if we have product suppliers to display { lstView.Items.Clear(); //start with empty list box foreach (Suppliers sup in suppliersList) { lstView.Items.Add(sup); } } else // null this supplier does not exist - need to refresh combo box { MessageBox.Show("There is no supplier to display."); } }
// UPDATE button click, visibility and read-only toggle private void btnProductEdit_Click(object sender, EventArgs e) { List <Products_Suppliers> currentProducts = new List <Products_Suppliers>(); btnAcceptEdit.Visible = true; btnProductEdit.Visible = false; pnlUpdate.Visible = true; txtProdName.ReadOnly = false; txtProdName.Enabled = true; lstSuppliers.Enabled = true; suppliersList = SuppliersDB.GetSuppliers(); foreach (Suppliers sup in suppliersList) { cboSupplier.Items.Add(sup); } }
private void btnUpdate_Click(object sender, EventArgs e) { var cells = dataGridView1.CurrentRow.Cells; string rowId = cells[0].Value.ToString(); string rowId1 = cells[1].Value.ToString(); //int rowId = Convert.ToInt32(cells[0].Value); var selectedSupplierID = SuppliersDB.GetSuppliers(rowId); frmModifySup modifySupplierFrm = new frmModifySup(); modifySupplierFrm.addSuppliers = false; modifySupplierFrm.modifySuppliers = selectedSupplierID; DialogResult result = modifySupplierFrm.ShowDialog(); if (result == DialogResult.OK) { dataGridView1.DataSource = SuppliersDB.GetSuppliers(); } }
// 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; }
// ------------Added by Wei Guang Yan---------------- // Radio-button-Click event on "Suppliers"----Edit/Add supplier private void radioButton2_CheckedChanged(object sender, EventArgs e) { // Set panels visibilities, show panel related to Supliers "edit" and "Add" //this.panel14.SendToBack(); //panelProductSuppliers.Hide();20190604 //Show panel13 this.panel13.BringToFront(); //this.panel13.Visible = true;20190604 // Set datasource of combobox to "Suppliers" comboBox2.DataSource = SuppliersDB.GetSuppliers(); comboBox2.DisplayMember = "SupName"; comboBox2.ValueMember = "SupplierId"; // Set desplay of relevant labels and buttons, and clear "Add" textbox panelStatus = PanelStatus.Suppliers; lblItemList.Text = "Suppliers"; lblUpdateItem.Text = "Update Supplier:"; btnUpdate.Text = "Update Supplier"; lblAddItem.Text = "Add Supplier:"; btnAdd.Text = "Add Supplier"; txtAddItem.Clear(); /*20190604 commented * RadioButton rb = sender as RadioButton; * if (rb != null) * { * if (rb.Checked) * { * // Only one radio button will be checked * //Console.WriteLine("Changed: " + rb.Name); * //datagridview2.source = DataLayer.SupplierDB.GetSuppliers(); * } * } */ }
private void fourBtnSave_Click(object sender, EventArgs e) { if (fourTab.SelectedIndex == 1) // EDIT MODE { // get current Supplier obj var currentSupplier = SuppliersDB.GetSuppliers() .Find(s => s.SupplierId == Convert.ToInt32(fourTxtEditSuppId.Text)); // initialize new Suppliers obj Suppliers newSupplier; // do validation: no empty name, no duplicated name if (Validator.TBIsEmpty(fourTxtEditSuppName, "Supplier Name") || FindDuplicatedSupplierName(fourTxtEditSuppName.Text)) { // validation failed, rollback to old name, do nothing fourTxtEditSuppName.Text = currentSupplier.SupName; fourTxtEditSuppName.SelectAll(); return; } else { // validation passed, create new Suppliers obj newSupplier = new Suppliers { SupName = fourTxtEditSuppName.Text } }; // try update try { int rowsAffected = SuppliersDB.UpdateSupplier(currentSupplier, newSupplier); MessageBox.Show($"{rowsAffected} record was successfully updated.", "Congratulations"); } catch (Exception ex) { MessageBox.Show($"Cannot complete update due to error: {ex.Message}."); } } else if (fourTab.SelectedIndex == 2) // ADD MODE { // validate input: no empty name, no duplicated name if (!Validator.TBIsEmpty(fourTxtAddSuppName, "Supplier Name") && Validator.TBHasNonNegativeInt(fourTxtAddSuppId, "Supplier Id") && !FindDuplicatedSupplierName(fourTxtAddSuppName.Text)) { // validation passed, create new Suppliers obj var newSupplier = new Suppliers { SupName = fourTxtAddSuppName.Text, // PK SuppId is not auto-increment, need to assign an new id SupplierId = Convert.ToInt32(fourTxtAddSuppId.Text) }; // try to add to DB try { int id = SuppliersDB.AddSupplier(newSupplier); MessageBox.Show($"New supplier was successfully added, supplier id: {id}.", "Congratulations"); fourTxtAddSuppId.Clear(); fourTxtAddSuppName.Clear(); } catch (Exception ex) { MessageBox.Show($"Cannot add new supplier due to duplicated id. Error detail: {ex.Message}."); } } } }
private void FrmSupplier_Load(object sender, EventArgs e) { dataGridView1.DataSource = SuppliersDB.GetSuppliers(); }
// generate id btn clicked: generate a new supplier id, fill in textbox private void fourBtnGenerateSuppId_Click(object sender, EventArgs e) { int newId = SuppliersDB.GetSuppliers().Max(s => s.SupplierId) + 1; fourTxtAddSuppId.Text = newId.ToString(); }
//builds datagrid on load private void frmSuppliers_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; suppliers = SuppliersDB.GetSuppliers(); supplierDataGridView.DataSource = suppliers; }
private void FrmSupplier_Load(object sender, EventArgs e) { dataGridView1.DataSource = SuppliersDB.GetSuppliers(); dataGridView1.Rows[0].Selected = true; }