Timofeev_pz8/shoe_store/admin.cs

144 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.EntityFrameworkCore;
using shoe_store.Models;
namespace shoe_store
{
public partial class admin : Form
{
Ispr2521TimofeevKoShoestoreContext context;
public admin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
avtorization avtorization = new avtorization();
avtorization.Show();
}
private void admin_Load(object sender, EventArgs e)
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
context = new Ispr2521TimofeevKoShoestoreContext();
context.Products.Include(x => x.Category).Include(x => x.Supplier).Include(x => x.Manufacturer).Load();
context.Database.EnsureCreated();
productBindingSource.DataSource = context.Products.Local.ToBindingList();
categoryBindingSource.DataSource = context.Categories.Local.ToBindingList();
supplierBindingSource.DataSource = context.Suppliers.Local.ToBindingList();
manufacturerBindingSource.DataSource = context.Manufacturers.Local.ToBindingList();
Populate();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
var form = new Formadd();
form.categoryBindingSourse.DataSource = categoryBindingSource.DataSource;
form.manufacturerBindingSourse.DataSource = manufacturerBindingSource.DataSource;
form.supplierBindingSource.DataSource = supplierBindingSource.DataSource;
form.productBindingSource.DataSource = productBindingSource.Current;
form.Save.Click += SaveButton_Click;
form.Show();
Populate();
}
private void SaveButton_Click(object sender, EventArgs e)
{
productBindingSource.EndEdit();
context.SaveChanges();
dataGridView1.Refresh();
Populate();
}
private void ex_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
var form = new Formadd();
form.categoryBindingSourse.DataSource = categoryBindingSource.DataSource;
form.manufacturerBindingSourse.DataSource = manufacturerBindingSource.DataSource;
form.supplierBindingSource.DataSource = supplierBindingSource.DataSource;
form.productBindingSource.DataSource = productBindingSource.AddNew();
var ob = form.productBindingSource.DataSource as Product;
form.Save.Click += (o, e) =>
{
if (!string.IsNullOrEmpty(ob?.ProductName))
{
context.SaveChanges();
}
else
{
MessageBox.Show("Название не должно быть пустым!");
}
}
;
form.Show();
Populate();
}
private void button4_Click(object sender, EventArgs e)
{
productBindingSource.RemoveCurrent();
context.SaveChanges();
dataGridView1.Refresh();
Populate();
}
private void cancel_Click(object sender, EventArgs e)
{
productBindingSource.CancelEdit();
}
private void Populate()
{
flowLayoutPanel1.Controls.Clear();
foreach (var binding in productBindingSource)
{
var control = new LayoutControl();
control.productBindingSource.DataSource = binding;
control.CategorSource1.DataSource = categoryBindingSource;
control.supplierbindingSource.DataSource = supplierBindingSource;
control.manuSource2.DataSource = manufacturerBindingSource;
flowLayoutPanel1.Controls.Add(control);
}
}
}
}