-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeSearch.cs
104 lines (99 loc) · 4.16 KB
/
TypeSearch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using JewelryStore.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JewelryStore.Forms
{
/// <summary>
/// Форма поиска по типу
/// </summary>
public partial class TypeSearch : Form
{
public TypeSearch()
{
InitializeComponent();
var context = new ApplicationDbContext();
//TODO получение уникальных типов из таблицы изделий
var types = context.Productions.Select(x => x.Type).Distinct().ToArray();
Type.Items.Clear();
Type.Items.AddRange(types);
Type.SelectedIndex = 0;
}
/// <summary>
/// Обновление таблицы
/// </summary>
/// <param name="t"></param>
public void RefreshGrid(string t)
{
Grid.Rows.Clear();
var context = new ApplicationDbContext();
//TODO получение всех ихделий
foreach (var m in context.Productions.Include(x=>x.MaterialCosts.Select(u=>u.Material)).Where(x=>x.Type==t).ToList())
{
Grid.Rows.Add(m.Id, m.Name,m.Weight,m.Cost);
}
}
/// <summary>
/// Вызов контекстного меню для таблицы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SalesMouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
int currentMouseOverRow = Grid.HitTest(e.X, e.Y).RowIndex;
if (currentMouseOverRow >= 0)
{
m.MenuItems.Add(new MenuItem("Просмотр", new EventHandler(delegate (Object o, EventArgs a)
{
int id = (int)Grid.Rows[currentMouseOverRow].Cells[0].Value;
var form = new ProductionForm(id,Models.EditMode.View);
form.ShowDialog();
})));
m.MenuItems.Add(new MenuItem("Редактировать", new EventHandler(delegate (Object o, EventArgs a)
{
int id = (int)Grid.Rows[currentMouseOverRow].Cells[0].Value;
var form = new ProductionForm(id, Models.EditMode.Edit);
form.ShowDialog();
})));
m.MenuItems.Add(new MenuItem("Удалить", new EventHandler(delegate (Object o, EventArgs a)
{
int id = (int)Grid.Rows[currentMouseOverRow].Cells[0].Value;
if (MessageBox.Show("Вы точно хотите удалить этот элемент?", "Удалить?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
var context = new ApplicationDbContext();
//TODO удаление записи по ключу
context.Productions.Remove(context.Productions.FirstOrDefault(x => x.Id == id));
context.SaveChanges();
}catch(Exception ex)
{
MessageBox.Show("Сначала необходимо удалить все связанные данные", "Ошибка");
}
}
})));
}
m.Show(Grid, new Point(e.X, e.Y));
}
}
/// <summary>
/// Поиск по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Search_Click(object sender, EventArgs e)
{
RefreshGrid((string)Type.SelectedItem);
}
}
}