private void add_Click(object sender, EventArgs e) { AddBuyerForm addBuyerForm = new AddBuyerForm(); int maxId = 0; if (addBuyerForm.ShowDialog() == DialogResult.OK) { try { DataRow[] rows = ds.buyers.Select(); foreach (DataRow r in rows) { if (Convert.ToInt32(r["id"]) > maxId) { maxId = Convert.ToInt32(r["id"]); } } DataRow row = ds.buyers.NewRow(); row["id"] = maxId + 1; row["name"] = addBuyerForm.BuyerName; row["address"] = addBuyerForm.Address; row["telephone"] = addBuyerForm.Telephone; ds.buyers.Rows.Add(row); ds.WriteXml("shop.xml"); if (buyersView.Rows.Count > 0) { buyersView.RowHeadersVisible = buyersView.ColumnHeadersVisible = edit.Enabled = delete.Enabled = true; } for (int i = 0; i < buyersView.Rows.Count; i++) { if (Convert.ToInt32(buyersView.Rows[i].Cells[0].Value) == maxId + 1) { buyersView.Rows[i].Cells[1].Selected = true; } } } catch (Exception) { MessageBox.Show("Ошибка добавления данных!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void sell_Click(object sender, EventArgs e) { SaleForm saleForm = new SaleForm(ds); saleForm.ChosenProductsList = chosenProductsList; if (saleForm.ShowDialog() == DialogResult.OK) { XmlTextWriter writer = new XmlTextWriter(saleForm.CheckFileName, System.Text.Encoding.UTF8); try { writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("sale"); writer.WriteElementString("date", saleForm.Date); writer.WriteElementString("buyerName", saleForm.BuyerName); writer.WriteElementString("sellerName", saleForm.SellerName); writer.WriteStartElement("products"); foreach (Product p in chosenProductsList) { DataRow[] rows = ds.products.Select("id = " + p.Id); rows[0]["count"] = Convert.ToInt32(rows[0]["count"]) - p.Count; ds.products.AcceptChanges(); ds.WriteXml("shop.xml"); writer.WriteStartElement("product"); writer.WriteElementString("name", p.Name); writer.WriteElementString("count", p.Count.ToString()); writer.WriteElementString("price", p.Price.ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteElementString("sum", saleForm.Sum); writer.WriteEndElement(); update_Click(sender, e); MessageBox.Show("Продажа успешно произведена!", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (writer != null) { writer.Close(); } } } }
private void add_Click(object sender, EventArgs e) { AddProductForm addProductForm = new AddProductForm(); int maxId = 0; if (addProductForm.ShowDialog() == DialogResult.OK) { try { DataRow[] rows = ds.products.Select(); foreach (DataRow r in rows) { if (Convert.ToInt32(r["id"]) > maxId) { maxId = Convert.ToInt32(r["id"]); } } DataRow row = ds.products.NewRow(); row["id"] = maxId + 1; row["name"] = addProductForm.ProductNameData; row["photo"] = Path.GetFileName(addProductForm.PhotoFileName); row["type"] = addProductForm.Type; row["category"] = addProductForm.Category; row["control"] = addProductForm.Control; row["consumption"] = addProductForm.Consumption; row["cameras"] = addProductForm.Cameras; row["carrangement"] = addProductForm.CArrangement; row["width"] = Math.Round(Convert.ToDouble(addProductForm.ProductWidth), 1); row["height"] = Math.Round(Convert.ToDouble(addProductForm.ProductHeight), 1); row["depth"] = Math.Round(Convert.ToDouble(addProductForm.ProductDepth), 1); if (addProductForm.RVolume.Length > 0) { row["rvolume"] = Convert.ToInt32(addProductForm.RVolume); } else { row["rvolume"] = DBNull.Value; } if (addProductForm.FVolume.Length > 0) { row["fvolume"] = Convert.ToInt32(addProductForm.FVolume); } else { row["fvolume"] = DBNull.Value; } row["rdefrosting"] = addProductForm.RDefrosting; row["fdefrosting"] = addProductForm.FDefrosting; row["power"] = Math.Round(Convert.ToDouble(addProductForm.Power), 1); row["noise"] = Math.Round(Convert.ToDouble(addProductForm.Noise), 1); row["price"] = Math.Round(Convert.ToDouble(addProductForm.Price), 2); row["count"] = addProductForm.Count; ds.products.Rows.Add(row); ds.WriteXml("shop.xml"); if (addProductForm.PhotoFileName.Length > 0) { if (!Directory.Exists("images")) { Directory.CreateDirectory("images"); } if (!File.Exists("images\\" + Path.GetFileName(addProductForm.PhotoFileName))) { File.Copy(addProductForm.PhotoFileName, "images\\" + Path.GetFileName(addProductForm.PhotoFileName)); } } if (productsView.Rows.Count > 0) { productsView.RowHeadersVisible = productsView.ColumnHeadersVisible = edit.Enabled = delete.Enabled = true; } for (int i = 0; i < productsView.Rows.Count; i++) { if (Convert.ToInt32(productsView.Rows[i].Cells[0].Value) == maxId + 1) { productsView.Rows[i].Cells[1].Selected = true; } } } catch (Exception) { MessageBox.Show("Ошибка добавления данных!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }