private void list_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { var getRowIndex = list.Rows[e.RowIndex].Cells[e.ColumnIndex].RowIndex; if (list.Rows[e.RowIndex].Cells[e.ColumnIndex].ColumnIndex == 9) { var result = MessageBox.Show("Are you sure you want to delete this record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { var getOrderItemID = Convert.ToInt64(list.Rows[e.RowIndex].Cells[7].Value); var partName = list.Rows[e.RowIndex].Cells[0].Value.ToString(); using (var context = new Session4Entities()) { var findPart = (from x in context.Parts where x.Name.Equals(partName) select new { x.ID, x.MinimumAmount }).First(); var findAmountTotal = (from x in context.OrderItems where x.PartID.Equals(findPart.ID) select x.Amount).Sum(); if ((findAmountTotal - Convert.ToDecimal(list.Rows[e.RowIndex].Cells[3].Value)) > findPart.MinimumAmount) { var toDelete = (from x in context.OrderItems where x.ID == getOrderItemID select x).First(); context.OrderItems.Remove(toDelete); context.SaveChanges(); list.Rows.RemoveAt(getRowIndex); GridFill(); } else { MessageBox.Show("Unable to delete item. Item does not even hit minimum required amount in all warehouses combined!", "Item amount is below required", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } else if (list.Rows[e.RowIndex].Cells[e.ColumnIndex].ColumnIndex == 8) { if (list.Rows[e.RowIndex].Cells[1].Value.ToString() == "Purchase Order") { var getOrderID = Convert.ToInt64(list.Rows[e.RowIndex].Cells[6].Value); (new POEdit(getOrderID)).ShowDialog(); GridFill(); } else if (list.Rows[e.RowIndex].Cells[1].Value.ToString() == "Warehouse Management") { var getOrderID = Convert.ToInt64(list.Rows[e.RowIndex].Cells[6].Value); (new WarehouseEdit(getOrderID)).ShowDialog(); GridFill(); } } } }
private void submit_Click(object sender, EventArgs e) { using (var context = new Session4Entities()) { var checkCurrect = (from x in context.Orders where x.ID == _oID select x).FirstOrDefault(); var getDestination = (from x in context.Warehouses where x.Name.Equals(warehouseBox.SelectedItem.ToString()) select x.ID).First(); var getSupplierID = (from x in context.Suppliers where x.Name.Equals(supplierBox.SelectedItem.ToString()) select x.ID).First(); if (checkCurrect != null) { var getCurentparts = (from x in context.OrderItems where x.OrderID == _oID select x); foreach (var item in getCurentparts) { context.OrderItems.Remove(item); } var currentOrder = (from x in context.Orders where x.ID.Equals(_oID) select new { x }).First(); currentOrder.x.SupplierID = getSupplierID; currentOrder.x.Date = date.Value; currentOrder.x.DestinationWarehouseID = getDestination; } else { context.Orders.Add(new Order() { Date = date.Value, SourceWarehouseID = null, DestinationWarehouseID = getDestination, SupplierID = getSupplierID, TransactionTypeID = 1, ID = _oID }); } var getRows = dataGridView1.RowCount; if (getRows > 0) { foreach (DataGridViewRow item in dataGridView1.Rows) { var getPartName = item.Cells[0].Value.ToString(); var findPartID = (from x in context.Parts where x.Name.Equals(getPartName) select x.ID).First(); context.OrderItems.Add(new OrderItem() { OrderID = _oID, Amount = Convert.ToDecimal(item.Cells[2].Value), BatchNumber = item.Cells[1].Value.ToString(), PartID = findPartID }); } context.SaveChanges(); this.Close(); } else { MessageBox.Show("Please insert at least 1 part into the purchase order!", "Purchase Order empty", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }