Dem_Timofeev_itog/DEM_Timofeev_prob/Form_admin.cs

302 lines
10 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DEM_Timofeev_prob.Models;
using Microsoft.EntityFrameworkCore;
namespace DEM_Timofeev_prob
{
public partial class Form_admin : Form
{
DemDemContext context;
public Form_admin()
{
InitializeComponent();
dataGridView1.DataError += (sender, e) => e.ThrowException = false;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
context = new DemDemContext();
context.Products.Include(x => x.IdTipeProductNavigation).Include(x => x.IdManufacturNavigation).Include(x => x.IdSuplierNavigation).Include(x => x.IdEdNavigation).Load();
context.Database.EnsureCreated();
bindingSource_product.DataSource = context.Products.Local.ToBindingList();
bindingSource_manufactur.DataSource = context.Manufacts.Local.ToBindingList();
bindingSource_ed.DataSource = context.Eds.Local.ToBindingList();
bindingSource_supplier.DataSource = context.Supliers.Local.ToBindingList();
bindingSource_tipe_product.DataSource = context.TipeProducts.Local.ToBindingList();
comboBox1.Items.AddRange(new string[]
{
"Все производители",
"а-я",
"я-а",
"цена а-я",
"цена я-а"
});
comboBox1.SelectedIndex = 0;
}
private void SaveProduct(Form_add form)
{
form.bindingSource_product.EndEdit();
var book = (Product)form.bindingSource_product.Current;
if (book.IdManufactur == 0)
{
MessageBox.Show("введите производителя");
return;
}
if (book.IdSuplier == 0)
{
MessageBox.Show("введите поставщика");
return;
}
if (book.IdEd == 0)
{
MessageBox.Show("введите единицу измерения");
return;
}
if (book.IdTipeProduct == 0)
{
MessageBox.Show("введите тип продукта");
return;
}
if (string.IsNullOrWhiteSpace(book.Articl))
{
MessageBox.Show("введите артикул");
return;
}
if (string.IsNullOrWhiteSpace(book.Name))
{
MessageBox.Show("введите название");
return;
}
if (string.IsNullOrWhiteSpace(book.ProductContent))
{
MessageBox.Show("введите описание");
return;
}
if (book.PriceProduct == 0)
{
MessageBox.Show("введите цену");
return;
}
if (book.KolVo == 0)
{
MessageBox.Show("введите количество");
return;
}
context.Products.Add(book);
context.SaveChanges();
bindingSource_product.DataSource = context.Products.ToList();
dataGridView1.Refresh();
MessageBox.Show("Товар сохранён!");
form.Close();
}
private void button_add_Click(object sender, EventArgs e)
{
this.Hide();
var form = new Form_add();
form.bindingSource_manufactur.DataSource = bindingSource_manufactur.DataSource;
form.bindingSource_supplier.DataSource = bindingSource_supplier.DataSource;
form.bindingSource_tipe_product.DataSource = bindingSource_tipe_product.DataSource;
form.bindingSource_ed.DataSource = bindingSource_ed.DataSource;
var pp = new Product();
form.bindingSource_product.DataSource = pp;
form.button_photo.Click += (s, args) =>
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
form.pictureBox_22.Image = Image.FromFile(openFileDialog.FileName);
var tovar = (Product)form.bindingSource_product.Current;
tovar.IdPhoto = File.ReadAllBytes(openFileDialog.FileName);
}
};
form.Save.Click += (s, args) => SaveProduct(form);
form.Show();
form.FormClosed += (s, args) => this.Show();
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
Form1 ff = new Form1();
ff.Show();
}
public void admin(string fio)
{
label3.Text = fio;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string qwery = textBox1.Text.Trim();
if (string.IsNullOrEmpty(qwery))
{
bindingSource_product.DataSource = context.Products.Local.ToBindingList();
}
var result = context.Products.Local
.Where(x => x.Name.IndexOf(qwery, StringComparison.OrdinalIgnoreCase) >= 0 ||
x.ProductContent.IndexOf(qwery, StringComparison.OrdinalIgnoreCase) >= 0 ||
x.IdManufacturNavigation.Name.IndexOf(qwery, StringComparison.OrdinalIgnoreCase) >= 0)
.ToList();
bindingSource_product.DataSource = result;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var data = context.Products.Local.AsQueryable();
switch (comboBox1.SelectedIndex)
{
case 1:
data = data.OrderBy(x => x.Name);
break;
case 2:
data = data.OrderByDescending(x => x.Name);
break;
case 3:
data = data.OrderBy(x => x.PriceProduct);
break;
case 4:
data = data.OrderByDescending(x => x.PriceProduct);
break;
}
bindingSource_product.DataSource = data.ToList();
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Вы уверены, что хотите удалить продукт?", "Подтверждение удаления", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
bindingSource_product.RemoveCurrent();
context.SaveChanges();
dataGridView1.Refresh();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
var form = new Form_redact();
form.bindingSource_manufactur.DataSource = bindingSource_manufactur.DataSource;
form.bindingSource_supplier.DataSource = bindingSource_supplier.DataSource;
form.bindingSource_tipe_product.DataSource = bindingSource_tipe_product.DataSource;
form.bindingSource_ed.DataSource = bindingSource_ed.DataSource;
form.bindingSource_product.DataSource = bindingSource_product.Current;
form.button_photo_45.Click += (s, args) =>
{
OpenFileDialog oo = new OpenFileDialog();
oo.Filter = "Image Files |*.img;*.jpeg";
if (oo.ShowDialog() == DialogResult.OK)
{
form.pictureBox_11.Image = Image.FromFile(oo.FileName);
var tovar = (Product)bindingSource_product.Current;
tovar.IdPhoto = File.ReadAllBytes(oo.FileName);
}
};
form.Save2.Click += (s, args) => SaveEdit(form);
form.FormClosed += (s, args) => this.Show();
form.Show();
}
private void SaveEdit(Form_redact form)
{
form.bindingSource_product.EndEdit();
var book = (Product)form.bindingSource_product.Current;
if (book.IdManufactur == 0)
{
MessageBox.Show("введите производителя");
return;
}
if (book.IdSuplier == 0)
{
MessageBox.Show("введите поставщика");
return;
}
if (book.IdEd == 0)
{
MessageBox.Show("введите единицу измерения");
return;
}
if (book.IdTipeProduct == 0)
{
MessageBox.Show("введите тип продукта");
return;
}
if (string.IsNullOrWhiteSpace(book.Articl))
{
MessageBox.Show("введите артикул");
return;
}
if (string.IsNullOrWhiteSpace(book.Name))
{
MessageBox.Show("введите название");
return;
}
if (string.IsNullOrWhiteSpace(book.ProductContent))
{
MessageBox.Show("введите описание");
return;
}
if (book.PriceProduct == 0)
{
MessageBox.Show("введите цену");
return;
}
if (book.KolVo == 0)
{
MessageBox.Show("введите количество");
return;
}
context.SaveChanges();
bindingSource_product.DataSource = context.Products.ToList();
dataGridView1.Refresh();
MessageBox.Show("Товар сохранён!");
form.Close();
}
private void button4_Click(object sender, EventArgs e)
{
this.Hide();
Form_order ff = new Form_order();
ff.Show();
}
}
}