private void DeleteItem() { var materialId = int.Parse(dgvInvoiceItems.SelectedRows[0].Cells[0].Value.ToString()); var deletedMaterialQuantity = decimal.Parse(dgvInvoiceItems.SelectedRows[0].Cells["Quantity"].Value .ToString()); PurchaseInvoiceItemVms.Remove(PurchaseInvoiceItemVms.Find(item => item.MaterialId == materialId)); MaterialsList.Find(material => material.Id == materialId).AvailableQuantity += deletedMaterialQuantity; FillGrid(); SetTotalPrice(); ResetInsertControls(); }
private void InsertItem() { ErrorProvider.Clear(); var isFormValid = true; if (cmbMaterials.SelectedIndex == 0) { isFormValid = false; ErrorProvider.SetError(cmbMaterials, Resources.ShouldSelectMaterial); } if (Math.Abs(dblInQuantity.Value) <= 0) { isFormValid = false; ErrorProvider.SetError(dblInQuantity, Resources.ThisFieldIsRequired); } if (Math.Abs(dblInUnitPrice.Value) <= 0) { isFormValid = false; ErrorProvider.SetError(dblInUnitPrice, Resources.ThisFieldIsRequired); } if (!isFormValid) { return; } var unitPrice = (decimal)dblInUnitPrice.Value; var addedQuantity = (decimal)dblInQuantity.Value; var materialId = int.Parse(cmbMaterials.SelectedValue.ToString()); if (PurchaseInvoiceItemVms.Exists(item => item.MaterialId == materialId)) { var purchaseInvoiceItem = PurchaseInvoiceItemVms.Find(item => item.MaterialId == materialId); purchaseInvoiceItem.Quantity += addedQuantity; } else { PurchaseInvoiceItemVms.Add(new PurchaseInvoiceItemVm { MaterialId = materialId, MaterialCode = MaterialsList.FirstOrDefault(material => material.Id == materialId)?.Code, MaterialName = ((KeyValuePair <int, string>)cmbMaterials.SelectedItem).Value, UnitPrice = unitPrice, Quantity = addedQuantity, Notes = txtNotes.Text.FullTrim() }); } MaterialsList.Find(material => material.Id == materialId).AvailableQuantity -= addedQuantity; FillGrid(); SetTotalPrice(); ResetInsertControls(); }