public void updateIngredientTable() { //save collumn whidt int[] col_sizes = new int[ingredientTable.Columns.Count]; for (int i = 0; i < ingredientTable.Columns.Count; i++) { col_sizes[i] = ingredientTable.Columns[i].Width; } if (controller.CurrentBase == Controller.INGREDIENTS) { return; } ingredientTable.Rows.Clear(); ingredientTable.Columns.Clear(); if (currentItem == null) { return; } if (currentItem.typeItem != controller.CurrentBase) { return; } //previously selected cell int x = 0; int y = 0; if (ingredientTable.CurrentCell != null) { x = ingredientTable.CurrentCell.ColumnIndex; y = ingredientTable.CurrentCell.RowIndex; } if (controller.CurrentBase == Controller.RECEIPTS) { Receipt receipt = (Receipt)currentItem; //add first item collumn with comboboxes and unit collumn DataGridViewComboBoxColumn itemCol = new DataGridViewComboBoxColumn(); itemCol.DataSource = controller.getAllItemNames(new int[] { Controller.INGREDIENTS }); itemCol.HeaderText = "Ingredientes"; itemCol.DataPropertyName = "item"; ingredientTable.Columns.Add(itemCol); ingredientTable.Columns.Add("unidade", "unidade"); ingredientTable.Columns[1].ReadOnly = true; //add size collumns for (int i = 0; i < receipt.sizes.Count; i++) { ingredientTable.Columns.Add(receipt.sizes.ElementAt(i), receipt.sizes[i]); } for (int i = 0; i < receipt.itemsForReceipt.Count; i++) { //build line for table string[] line = new string[receipt.sizes.Count + 2]; line[0] = receipt.itemsForReceipt.ElementAt(i).name; line[1] = ((Ingredient)receipt.itemsForReceipt.ElementAt(i)).unit; for (int j = 0; j < receipt.sizes.Count; j++) { line[j + 2] = receipt.quantities.ElementAt(j).ElementAt(i).ToString(); } ingredientTable.Rows.Add(line); } } else { Order order = (Order)currentItem; //add first item collumn with comboboxes and unit collumn DataGridViewComboBoxColumn itemCol = new DataGridViewComboBoxColumn(); itemCol.DataSource = controller.getAllItemNames(new int[] { Controller.INGREDIENTS, Controller.RECEIPTS }); itemCol.HeaderText = "Itens"; itemCol.DataPropertyName = "item"; //add secondly sizes columns costumized by receipt sizes DataGridViewComboBoxColumn sizeColumn = new DataGridViewComboBoxColumn(); sizeColumn.HeaderText = "Tamanho"; sizeColumn.DataPropertyName = "tamanho"; ingredientTable.Columns.Add(itemCol); ingredientTable.Columns.Add(sizeColumn); ingredientTable.Columns.Add("quantidade", "quantidade"); ingredientTable.Columns.Add("valor", "valor"); ingredientTable.Columns[3].ReadOnly = true; for (int i = 0; i < order.items.Count; i++) { //build line for table string[] line = new string[4]; line[0] = order.items.ElementAt(i).name; line[1] = order.sizes.ElementAt(i); line[2] = order.quantities.ElementAt(i).ToString(); ingredientTable.Rows.Add(line); //build costumized datasource DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)ingredientTable.Rows[i].Cells[1]; if (order.items.ElementAt(i).typeItem == Controller.RECEIPTS) { cell.DataSource = ((Receipt)order.items.ElementAt(i)).sizes.ToArray(); } else { cell.DataSource = new string[] { "" }; } } priceOrder(); } //select row of table if ((ingredientTable.Rows.Count > 0) && (ingredientTable.Columns.Count > 0)) { x = Math.Min(Math.Max(0, x), ingredientTable.Columns.Count - 1); y = Math.Min(Math.Max(0, y), ingredientTable.Rows.Count - 1); ingredientTable.ClearSelection(); ingredientTable.CurrentCell = ingredientTable.Rows[y].Cells[x]; } //set back column whidts for (int i = 0; i < Math.Min(col_sizes.Length, ingredientTable.Columns.Count); i++) { ingredientTable.Columns[i].Width = col_sizes[i]; } }
public void ingredientTableCellChanged(object sender, DataGridViewCellEventArgs e) { //changes in receipt base if (e.ColumnIndex == 0) { //change target item and its information string value = (string)ingredientTable.Rows[e.RowIndex].Cells[0].Value; Item targetItem = controller.getItemByName(value); if (targetItem != null) { if (controller.CurrentBase == Controller.RECEIPTS) { Receipt receipt = (Receipt)currentItem; receipt.itemsForReceipt.RemoveAt(e.RowIndex); receipt.itemsForReceipt.Insert(e.RowIndex, targetItem); ingredientTable.Rows[e.RowIndex].Cells[1].Value = ((Ingredient)targetItem).unit; } else if (currentItem.typeItem == Controller.ORDERS || currentItem.typeItem == Controller.PRODUCTS) { Order order = (Order)currentItem; if (targetItem.typeItem == Controller.RECEIPTS) { if (((Receipt)targetItem).sizes.Count == 0) { MessageBox.Show("O item selecionado não possui informações", "Erro: item inválido"); ingredientTable.Rows[e.RowIndex].Cells[0].Value = order.items.ElementAt(e.RowIndex).name; return; } } order.items.RemoveAt(e.RowIndex); order.sizes.RemoveAt(e.RowIndex); order.items.Insert(e.RowIndex, targetItem); if (targetItem.typeItem == Controller.RECEIPTS) { order.sizes.Insert(e.RowIndex, ((Receipt)targetItem).sizes.ElementAt(0)); DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)ingredientTable.Rows[e.RowIndex].Cells[1]; cell.DataSource = ((Receipt)targetItem).sizes.ToArray(); cell.Value = order.sizes[e.RowIndex].ToString(); } else { order.sizes.Insert(e.RowIndex, ""); DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)ingredientTable.Rows[e.RowIndex].Cells[1]; cell.DataSource = new string[] { "" }; cell.Value = order.sizes[e.RowIndex].ToString(); } } } } else if (e.ColumnIndex == 1) { if (currentItem.typeItem == Controller.ORDERS || currentItem.typeItem == Controller.PRODUCTS) { Order order = (Order)currentItem; order.sizes.RemoveAt(e.RowIndex); order.sizes.Insert(e.RowIndex, (string)ingredientTable.Rows[e.RowIndex].Cells[1].Value); } } else if (e.ColumnIndex > 1) { //manipulate quantity on list - first check if typed value is a valid number if (currentItem == null) { return; } if (currentItem.typeItem == Controller.INGREDIENTS) { return; } if (controller.CurrentBase == Controller.RECEIPTS) { Receipt receipt = (Receipt)currentItem; float value = 0; if (float.TryParse((string)ingredientTable.Rows[e.RowIndex].Cells[e.ColumnIndex].Value, out value)) { receipt.quantities.ElementAt(e.ColumnIndex - 2).RemoveAt(e.RowIndex); receipt.quantities.ElementAt(e.ColumnIndex - 2).Insert(e.RowIndex, value); } ingredientTable.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = receipt.quantities.ElementAt(e.ColumnIndex - 2).ElementAt(e.RowIndex).ToString(); } if (currentItem.typeItem == Controller.ORDERS || currentItem.typeItem == Controller.PRODUCTS) { Order order = (Order)currentItem; if (e.ColumnIndex == 2) // quantity column { float value = 0; if (float.TryParse((string)ingredientTable.Rows[e.RowIndex].Cells[2].Value, out value)) { order.quantities.RemoveAt(e.RowIndex); order.quantities.Insert(e.RowIndex, value); } } ingredientTable.Rows[e.RowIndex].Cells[2].Value = order.quantities.ElementAt(e.RowIndex).ToString(); } } if (currentItem.typeItem == Controller.ORDERS || currentItem.typeItem == Controller.PRODUCTS) { priceOrder(); } else { Receipt receipt = (Receipt)currentItem; if (receipt.sizes.Count > 0) { float value = receipt.priceItem(receipt.sizes.ElementAt(0), receipt.yield); value = (float)Math.Round(value, 2); priceBox.Value = (decimal)value; int pos = controller.onSpot.IndexOf(currentItem); databaseTable.Rows[pos].Cells[2].Value = value.ToString(); } } }