private void btnNewProduct_Click(object sender, EventArgs e) { ProductInfoForm newProduct = new ProductInfoForm(infoType, ref productList); if (newProduct.ShowDialog() == System.Windows.Forms.DialogResult.OK) { ///// START TO SAVE HERE List<Product> prodList = Space.GetProductList(); if(infoType == Space.InfoType.AMR) prodList = Space.GetProductListAmr(); prodList.Add(new Product(newProduct.ProductName, newProduct.ProductPrice, newProduct.ProductUnit, newProduct.ProductLocation, newProduct.ProductOperative)); this.productList = prodList; SaveDataToPhysicalDrive(); ListAllProduct(); dgvGeneral.FirstDisplayedScrollingRowIndex = dgvGeneral.RowCount - 1; } newProduct.Dispose(); }
private void dgvGeneral_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) return; string prodUnit = dgvGeneral.Rows[e.RowIndex].Cells[0].Value.ToString(); string prodName = dgvGeneral.Rows[e.RowIndex].Cells[1].Value.ToString(); double prodPrice = double.Parse(dgvGeneral.Rows[e.RowIndex].Cells[2].Value.ToString()); int pinIdx = -1; if (e.ColumnIndex == 5) { var p = productList.Where(ex => ex.Name == prodName && ex.Unit == prodUnit).SingleOrDefault(); if(p == null) throw new Exception("How can it reach this regeon"); ProductInfoForm prodInfoForm = new ProductInfoForm(infoType, ref productList, p); if (prodInfoForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // REMOVE OLD DATA AND ADD A NEW OBJECT pinIdx = productList.IndexOf(p); if (pinIdx == -1) throw new Exception("How can it reach this regeon"); productList.RemoveAt(pinIdx); // ADD A NEW PRODUCT AT A CERTAIN LOCATION Product newProduct = prodInfoForm.GetProductObject(); productList.Insert(pinIdx, newProduct); ////// CHECK FOR ROGUE DATA int redundant = productList.Where(ex => ex.Name == newProduct.Name && ex.Unit == newProduct.Unit).Count(); if (redundant > 1) { MessageBox.Show("คุณใสข้อมูลที่ทับซ้อนกับข้อมูลที่มีอยู่แล้ว", "ข้อมูลไม่ถูกต้อง"); productList.RemoveAt(pinIdx); productList.Insert(pinIdx, p); } } prodInfoForm.Dispose(); ListAllProduct(); dgvGeneral.FirstDisplayedScrollingRowIndex = pinIdx; } else if (e.ColumnIndex == 6) { string message = "คุณต้องการลบข้อมูลของ " + prodName + " หน่วย" + prodUnit + " ใช่หรือไม่"; if (MessageBox.Show(message, "กรุณายืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { //// DELETE RECORD var delItem = productList.Where(ex => ex.Name == prodName && ex.Unit == prodUnit).SingleOrDefault(); if (delItem == null) throw new Exception("How can it reach this regeon"); pinIdx = productList.IndexOf(delItem) - 1; if (pinIdx < 0) pinIdx = 0; productList.Remove(delItem); ListAllProduct(); dgvGeneral.FirstDisplayedScrollingRowIndex = pinIdx; } } }