forked from FulLachy/Teamerino-Memerino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormInventory.cs
91 lines (83 loc) · 3.31 KB
/
FormInventory.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
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 Teamerino_Memerino
{
public partial class FormInventory : Form
{
public FormInventory()
{
InitializeComponent();
}
private void Inventory_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
Database.Instance.WriteItems("...//...//Resources//Items//inventory.txt", DGV_Inv);
}
private void FormInventory_Load(object sender, EventArgs e)
{
Database.Instance.BindInventoryToDVG(DGV_Inv);
}
private void bt_Add_Inv_Click(object sender, EventArgs e)
{
Database.Instance.AddItem(new InventoryStruct());
}
private void remove_Click(object sender, EventArgs e)
{
if (DGV_Inv.CurrentRow == null) return;
var item = (InventoryStruct)DGV_Inv.CurrentRow.DataBoundItem;
var confirmation = MessageBox.Show("Are you sure you want to remove " + item.ItemName + " from database?", "Confirm Removal", MessageBoxButtons.YesNo);
if (confirmation == DialogResult.Yes)
{
Database.Instance.RemoveItem(item);
}
}
private void invalidate_cell(DataGridView dgv, DataGridViewCell cell, DataGridViewCellValidatingEventArgs e, String message)
{
cell.Value = cell.Value;
e.Cancel = true;
((DataGridView)dgv).Rows[e.RowIndex].ErrorText = message;
}
private void dgv_Cell_Validating(object sender, DataGridViewCellValidatingEventArgs e)
{
var dgv = ((DataGridView)sender);
string header = dgv.Columns[e.ColumnIndex].HeaderText;
var cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
try
{
if (header.Equals("Barcode") || header.Equals("Stock") || header.Equals("LowStockLevel"))
{
var c0 = 0;
if (e.FormattedValue == null || !int.TryParse(e.FormattedValue.ToString(), out c0))
invalidate_cell(dgv, cell, e, header + " Must Be an Integer");
if(c0 < 0)
invalidate_cell(dgv, cell, e, header + " Must Be Positive");
}
else if (header.Equals("Price"))
{
double c0 = 0;
if (e.FormattedValue == null || !double.TryParse(e.FormattedValue.ToString(), out c0))
invalidate_cell(dgv, cell, e, header + " Must be a Number, don't include $ at the start");
}
}
catch
{
invalidate_cell(dgv, cell, e, header + " Unhandled error");
}
}
private void DGV_Inv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
((DataGridView)sender).Rows[e.RowIndex].ErrorText = String.Empty;
}
}
}