131 lines
4.7 KiB
C#
131 lines
4.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
|
|
namespace samusev_42
|
|
{
|
|
public partial class MainCardForm : Form
|
|
{
|
|
Ispr2522SamusevOvLazarev2Context context;
|
|
|
|
public MainCardForm()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
base.OnLoad(e);
|
|
context = new Ispr2522SamusevOvLazarev2Context();
|
|
|
|
context.Products.Include(x => x.IdCategoryNavigation).Load();
|
|
context.Products.Include(x => x.IdManufacturerNavigation).Load();
|
|
context.Products.Include(x => x.IdProductNameNavigation).Load();
|
|
context.Products.Include(x => x.IdSupplierNavigation).Load();
|
|
context.Categories.Load();
|
|
context.Manufacturers.Load();
|
|
context.ProductNames.Load();
|
|
context.Suppliers.Load();
|
|
|
|
productBindingSource.DataSource = context.Products.Local.ToBindingList();
|
|
categorySource.DataSource = context.Categories.Local.ToBindingList();
|
|
manufacturerSource.DataSource = context.Manufacturers.Local.ToBindingList();
|
|
product_nameSource.DataSource = context.ProductNames.Local.ToBindingList();
|
|
SupplierSource.DataSource = context.Suppliers.Local.ToBindingList();
|
|
|
|
Populate();
|
|
}
|
|
|
|
public void Populate()
|
|
{
|
|
flowLayoutPanel1.Controls.Clear();
|
|
foreach (var item in productBindingSource)
|
|
{
|
|
if (item is Product p)
|
|
{
|
|
CardForm card = new CardForm(p);
|
|
card.Click += (s, ev) => { productBindingSource.Position = productBindingSource.IndexOf(p); };
|
|
flowLayoutPanel1.Controls.Add(card);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void SaveCardButton_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void CardEditButton_Click_1(object sender, EventArgs e)
|
|
{
|
|
if (productBindingSource.Current == null) return;
|
|
|
|
var form = new EditForm();
|
|
form.categoryBindingSource.DataSource = categorySource.DataSource;
|
|
form.manufacturerBindingSource.DataSource = manufacturerSource.DataSource;
|
|
form.productNameBindingSource.DataSource = product_nameSource.DataSource;
|
|
form.supplierBindingSource.DataSource = SupplierSource.DataSource;
|
|
form.productBindingSource.DataSource = productBindingSource.Current;
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
productBindingSource.EndEdit();
|
|
context.SaveChanges();
|
|
Populate();
|
|
}
|
|
}
|
|
|
|
private void CardDeleteButton_Click_1(object sender, EventArgs e)
|
|
{
|
|
if (productBindingSource.Current == null) return;
|
|
|
|
var result = MessageBox.Show("Удалить этот товар?", "Подтверждение", MessageBoxButtons.YesNo);
|
|
if (result == DialogResult.Yes)
|
|
{
|
|
productBindingSource.RemoveCurrent();
|
|
context.SaveChanges();
|
|
Populate();
|
|
}
|
|
}
|
|
|
|
private void CardAddButton_Click_1(object sender, EventArgs e)
|
|
{
|
|
var newProduct = new Product();
|
|
newProduct.Article = "";
|
|
newProduct.Unit = "шт.";
|
|
newProduct.Description = "-";
|
|
newProduct.Photo = "";
|
|
newProduct.Price = 0;
|
|
newProduct.Count = 0;
|
|
newProduct.Discount = 0;
|
|
|
|
var form = new EditForm();
|
|
form.categoryBindingSource.DataSource = categorySource;
|
|
form.manufacturerBindingSource.DataSource = manufacturerSource;
|
|
form.productNameBindingSource.DataSource = product_nameSource;
|
|
form.supplierBindingSource.DataSource = SupplierSource;
|
|
form.productBindingSource.DataSource = newProduct;
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
if (newProduct.IdProductName <= 0)
|
|
throw new System.ComponentModel.DataAnnotations.ValidationException("Выберите название товара!");
|
|
|
|
context.Products.Add(newProduct);
|
|
context.SaveChanges();
|
|
|
|
Populate();
|
|
MessageBox.Show("Товар добавлен!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
context.Entry(newProduct).State = EntityState.Detached;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |