-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmCategory.cs
140 lines (117 loc) · 4.58 KB
/
frmCategory.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Linq;
using System.Windows.Forms;
namespace BookShopManager
{
public partial class frmCategory : Form
{
public frmCategory()
{
InitializeComponent();
}
private void categoryTblBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
UpDateData();
}
private void UpDateData()
{
this.Validate();
this.categoryTblBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.bOOKSHOPSDBDataSet);
}
private void frmCategory_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'bOOKSHOPSDBDataSet.CategoryTbl' table. You can move, or remove it, as needed.
this.categoryTblTableAdapter.Fill(this.bOOKSHOPSDBDataSet.CategoryTbl);
}
BookShopDataContext db = new BookShopDataContext();
private void AddCategory()
{
if (txtCate.Text.Trim() == "")
{
MessageBox.Show("Please enter category name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
string category = txtCate.Text.Trim();
bool categoryExists = db.CategoryTbls.Any(c => c.Category == category);
if (categoryExists)
{
MessageBox.Show("Category already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
CategoryTbl categoryTbl = new CategoryTbl();
categoryTbl.Category = category;
db.CategoryTbls.InsertOnSubmit(categoryTbl);
db.SubmitChanges();
MessageBox.Show("Category added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnAddCategory_Click(object sender, EventArgs e)
{
AddCategory();
this.categoryTblTableAdapter.Fill(this.bOOKSHOPSDBDataSet.CategoryTbl);
}
private void DeletCate()
{
if (MessageBox.Show("Are you sure want to delete?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (dvCategory.SelectedCells.Count > 0)
{
string id = dvCategory.SelectedCells[1].Value.ToString();
CategoryTbl cate = db.CategoryTbls.Where(pv => pv.Category.Equals(id)).FirstOrDefault();
if (cate != null)
{
db.CategoryTbls.DeleteOnSubmit(cate);
db.SubmitChanges();
this.categoryTblTableAdapter.Fill(this.bOOKSHOPSDBDataSet.CategoryTbl);
}
}
}
}
private void btnDeleCategory_Click(object sender, EventArgs e)
{
DeletCate();
}
//todo: Them nut sua
private void EditCategory()
{
if (dvCategory.SelectedCells.Count > 0)
{
string id = dvCategory.SelectedCells[1].Value.ToString();
CategoryTbl cate = db.CategoryTbls.Where(pv => pv.Category.Equals(id)).FirstOrDefault();
if (cate != null)
{
cate.Category = txtCate.Text;
db.SubmitChanges();
this.categoryTblTableAdapter.Fill(this.bOOKSHOPSDBDataSet.CategoryTbl);
MessageBox.Show("Category edited successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Category not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please select a category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
private void categoryTblBindingSource_PositionChanged(object sender, EventArgs e)
{
txtCate.DataBindings.Clear();
txtCate.DataBindings.Add("Text", categoryTblBindingSource, "Category");
}
}
}