private void addComponentsOnForm(NewProductForm newProductForm, Product product) { List <int> comboBoxValues = db.data.Product_Component.Where(pc => pc.Product_ref == product.ProductId) .Select(pc => pc.Component_ref).ToList(); List <decimal> componentsMass = db.data.Product_Component.Where(pc => pc.Product_ref == product.ProductId) .Select(pc => pc.PartMass).ToList(); int numComponents = comboBoxValues.Count; GlobalInfo.constituents[0].comboBox.SelectedValue = comboBoxValues[0]; GlobalInfo.constituents[0].textBox.Text = componentsMass[0].ToString(); for (int i = 1; i < numComponents; i++) { GlobalInfo.constituents[i].componentLabel.Visible = true; GlobalInfo.constituents[i].comboBox.Visible = true; GlobalInfo.constituents[i].massLabel.Visible = true; GlobalInfo.constituents[i].textBox.Visible = true; GlobalInfo.constituents[i].comboBox.SelectedValue = comboBoxValues[i]; GlobalInfo.constituents[i].textBox.Text = componentsMass[i].ToString(); newProductForm.deleteComponentButton.Visible = true; newProductForm.deleteComponentButton.Location = new Point(newProductForm.deleteComponentButton.Location.X, GlobalInfo.constituents[i].textBox.Location.Y); newProductForm.AddComponent.Location = new Point(newProductForm.AddComponent.Location.X, GlobalInfo.constituents[i].textBox.Location.Y + 40); newProductForm.Add_OK_btn.Location = new Point(newProductForm.Add_OK_btn.Location.X, newProductForm.AddComponent.Location.Y + 40); newProductForm.Add_Cancel_btn.Location = new Point(newProductForm.Add_Cancel_btn.Location.X, newProductForm.AddComponent.Location.Y + 40); newProductForm.groupBox.Size = new Size(newProductForm.groupBox.Width, newProductForm.Add_Cancel_btn.Location.Y + 40); newProductForm.Size = new Size(newProductForm.Size.Width, newProductForm.groupBox.Size.Height + 40); ++Constituents.numComponents; if (Constituents.numComponents == 10) { newProductForm.AddComponent.Visible = false; } } }
private void AlertBtn_Click(object sender, EventArgs e) { try { if (GlobalInfo.currentUser.Rule.LevelAccess != "менеджер") { MessageBox.Show("У вас недостатньо прав, щоб здійснити дану операцію!"); return; } if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (!converted) { return; } Product newProduct = db.data.Products.Find(id); var newProductForm = new NewProductForm(); if (!newProduct.IsAvaliable) { if (db.data.Order_Product.Any(op => op.Product_ref == newProduct.ProductId)) { newProductForm.IsAvaibleCheckBox.Enabled = false; } } initializeComboBoxes(); newProductForm.TypeComboBox.DataSource = db.data.Types.ToList(); newProductForm.TypeComboBox.ValueMember = "TypeId"; newProductForm.TypeComboBox.DisplayMember = "TypeName"; newProductForm.PriceTextBox.Text = newProduct.Price.ToString(); newProductForm.StoreIdTextBox.Text = newProduct.Store_ref.ToString(); newProductForm.IsAvaibleCheckBox.Checked = newProduct.IsAvaliable; newProductForm.TypeComboBox.SelectedValue = newProduct.Type_ref; addComponentsOnForm(newProductForm, newProduct); do { DialogResult result = newProductForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } if (newProductForm.PriceTextBox.Text.Trim() == "" || newProductForm.StoreIdTextBox.Text.Trim() == "" || !checkMassEntered()) { MessageBox.Show("Усі поля повинні бути заповнені!"); continue; } else if (!checkUniqueness()) { MessageBox.Show("Усі компоненти повинні бути різного виду!"); continue; } else if (!Int32.TryParse(newProductForm.StoreIdTextBox.Text.Trim(), out int n)) { MessageBox.Show("Неправильний формат Id магазину. Id повонине бути числом."); continue; } int StoreId = Int32.Parse(newProductForm.StoreIdTextBox.Text); if (db.data.Stores.Where(s => s.ObjectId == StoreId).Count() == 0) { MessageBox.Show("Tакого Id магазину не існує, спробуйте інше"); } else if (!float.TryParse(newProductForm.PriceTextBox.Text, out float n)) { MessageBox.Show("Неправильно вказана ціна товару. Ціна повинн бути числом."); } else if (!checkMassFloat()) { MessageBox.Show("Непраильно вказана маса складника. Маса повинна бути числом."); } else { break; } } while (true); db.data.Product_Component.RemoveRange(db.data.Product_Component .Where(pc => pc.Product_ref == newProduct.ProductId)); newProduct.Price = decimal.Parse(newProductForm.PriceTextBox.Text); newProduct.Store_ref = Int32.Parse(newProductForm.StoreIdTextBox.Text); newProduct.IsAvaliable = newProductForm.IsAvaibleCheckBox.Checked; newProduct.Type_ref = Int32.Parse(newProductForm.TypeComboBox.SelectedValue.ToString()); newProduct.TotalMass = getProductMass(); db.data.Entry(newProduct).State = EntityState.Modified; Product_Component prod_com; for (int i = 0; i < Constituents.numComponents; i++) { prod_com = new Product_Component(); prod_com.Product_ref = newProduct.ProductId; prod_com.Component_ref = Int32.Parse(GlobalInfo.constituents[i].comboBox.SelectedValue.ToString()); prod_com.PartMass = decimal.Parse(GlobalInfo.constituents[i].textBox.Text); db.data.Product_Component.Add(prod_com); } db.data.SaveChanges(); dataGridView1.Refresh(); countStoreProductCost(Int32.Parse(newProduct.Store_ref.ToString())); } } catch (Exception exception) { MessageBox.Show("Some error occured: " + exception.Message + " - " + exception.Source); throw; } }