131 lines
2.7 KiB
C#
131 lines
2.7 KiB
C#
using Diplom_B.DB;
|
|
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;
|
|
|
|
namespace Diplom_B
|
|
{
|
|
public partial class IzdForm : Form
|
|
{
|
|
public IzdForm()
|
|
{
|
|
InitializeComponent();
|
|
errorLable.Visible = false;
|
|
try
|
|
{
|
|
using (var db = new MainDB()){
|
|
var res = (from a in db.Izdeliya
|
|
select a).ToArray();
|
|
UpdateTable(res);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
ShowError();
|
|
}
|
|
}
|
|
|
|
|
|
private void createButton_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (!double.TryParse(cenaBox.Text, out double cena))
|
|
{
|
|
ShowError("Ошибка цены.");
|
|
return;
|
|
}
|
|
if (!int.TryParse(otdRazBox.Text, out int otdRaz))
|
|
{
|
|
ShowError("Ошибка отдела-разработчика.");
|
|
return;
|
|
}
|
|
|
|
using (var db = new MainDB())
|
|
{
|
|
var r = new Izdelie()
|
|
{
|
|
Name = nameBox.Text,
|
|
DecNum = decBox.Text,
|
|
Shifr = shifrBox.Text,
|
|
Litera = literaBox.Text,
|
|
Cena = cena,
|
|
OtdelRazrab = otdRaz,
|
|
Ved = vedBox.Text,
|
|
GlavKonstr = glavKonstrBox.Text
|
|
};
|
|
db.Izdeliya.Add(r);
|
|
db.SaveChanges();
|
|
var izd = (from a in db.Izdeliya
|
|
select a).ToArray();
|
|
UpdateTable(izd);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ShowError(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
private void UpdateTable(Izdelie[] arr)
|
|
{
|
|
{
|
|
var r = dataGridView1.Rows;
|
|
while (r.Count > 0)
|
|
r.Remove(r[0]);
|
|
var c = dataGridView1.Columns;
|
|
while (c.Count > 0)
|
|
c.Remove(c[0]);
|
|
}
|
|
{
|
|
var c = dataGridView1.Columns;
|
|
c.Add("Id", "№");
|
|
c["Id"].Width = 4;
|
|
c.Add("Name", "Наименование");
|
|
c.Add("DecNum", "Децимальный №");
|
|
c.Add("Shifr", "Шифр");
|
|
c.Add("Litera", "Литера");
|
|
c.Add("Cena", "Цена");
|
|
c.Add("OtdelRazrab", "Отдел");
|
|
c.Add("Ved", "Ведущий");
|
|
c.Add("GlavKonstr", "Главный конструктор");
|
|
}
|
|
{
|
|
var r = dataGridView1.Rows;
|
|
foreach(var izd in arr)
|
|
r.Add(new object[] {
|
|
izd.Id,
|
|
izd.Name,
|
|
izd.DecNum,
|
|
izd.Shifr,
|
|
izd.Litera,
|
|
izd.Cena,
|
|
izd.OtdelRazrab,
|
|
izd.Ved,
|
|
izd.GlavKonstr
|
|
});
|
|
}
|
|
}
|
|
private void ShowError(string msg = null)
|
|
{
|
|
errorLable.Text = string.IsNullOrEmpty(msg) ? "Неизвестная ошибка." : msg;
|
|
errorLable.Visible = true;
|
|
new Task(() => {
|
|
Task.Delay(5000).Wait();
|
|
if (InvokeRequired) Invoke((Action)(() => { errorLable.Visible = false; }));
|
|
else errorLable.Visible = false;
|
|
}).Start();
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|