-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
236 lines (209 loc) · 6.68 KB
/
MainForm.cs
File metadata and controls
236 lines (209 loc) · 6.68 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using Microsoft.Data.SqlClient;
using System;
using System.Buffers.Text;
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 static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace Managment
{
public partial class MainForm : Form
{
bool menuExpand = false;
bool sideBarExpand = true;
SqlConnection conn = new SqlConnection(StaticVariables.connectionString);
Form1 loginForm;
private int rowIndex = 0;
string tableName;
private int tableRows = 0;
bool fromLogout = false;
public MainForm()
{
InitializeComponent();
}
public MainForm(String username, Form1 loginF)
{
InitializeComponent();
label2.Text = username;
loginForm = loginF;
}
private void itemMenuTransition_Tick(object sender, EventArgs e)
{
if (!menuExpand)
{
itemPanel.Visible = true;
itemMenu.Height += 10;
if (itemMenu.Height >= 232)
{
itemMenuTransition.Stop();
menuExpand = true;
}
}
else
{
dataGridView1.DataSource = null;
ClearValues();
itemPanel.Visible = false;
itemMenu.Height -= 10;
if (itemMenu.Height <= 50)
{
itemMenuTransition.Stop();
menuExpand = false;
}
}
}
private void sideBarTransition_Tick(object sender, EventArgs e)
{
if (sideBarExpand)
{
sideBarMenu.Width -= 10;
if (sideBarMenu.Width <= 55)
{
sideBar.Stop();
sideBarExpand = false;
}
}
else
{
sideBarMenu.Width += 10;
if (sideBarMenu.Width >= 191)
{
sideBar.Stop();
sideBarExpand = true;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
itemMenuTransition.Start();
}
private void sideBarToggle_Click(object sender, EventArgs e)
{
sideBar.Start();
}
private async void FillDataGrid(string db_TableName)
{
ClearValues();
try
{
string sqlQuery = $"SELECT * FROM {db_TableName}";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
await conn.OpenAsync(); // Open the connection asynchronously
SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
tableRows = dt.Rows.Count;
}
tableName = db_TableName;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
{
await conn.CloseAsync(); // Close the connection asynchronously
}
}
}
private void basesSubOption_Click(object sender, EventArgs e)
{
FillDataGrid(StaticVariables.baseDB);
}
private void mattressSubOption_Click(object sender, EventArgs e)
{
FillDataGrid(StaticVariables.mattressDB);
}
private void fabricsSubOption_Click(object sender, EventArgs e)
{
FillDataGrid(StaticVariables.fabricDB);
}
private void button1_Click(object sender, EventArgs e)
{
fromLogout = true;
Close();
loginForm.Show();
loginForm.ClearValues();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (!fromLogout)
loginForm.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// Ensure the click is within the grid and not on header cells
if (e.RowIndex >= 0)
{
rowIndex = e.RowIndex;
editButton.Enabled = true;
deleteButton.Enabled = true;
newItem.Enabled = false;
}
}
private void clearSelection_Click(object sender, EventArgs e)
{
ClearValues();
}
private void ClearValues()
{
rowIndex = 0;
editButton.Enabled = false;
deleteButton.Enabled = false;
newItem.Enabled = true;
}
private async void deleteButton_Click(object sender, EventArgs e)
{
var dataGridItem = dataGridView1.Rows[rowIndex].Cells[0].Value;
if (dataGridItem == null) return;
int rowId = (int)dataGridItem;
try
{
string sqlQuery = $"DELETE FROM {tableName} WHERE Id = @id";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
await conn.OpenAsync();
cmd.Parameters.AddWithValue("@id", rowId);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
{
await conn.CloseAsync();
}
FillDataGrid(tableName);
ClearValues();
}
}
private void newItem_Click(object sender, EventArgs e)
{
NewItemForm newItemForm = new NewItemForm(0);
newItemForm.ShowDialog();
if(dataGridView1.DataSource != null)
FillDataGrid(tableName);
}
private void EditButton_Click(object sender, EventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[rowIndex];
if (row.Cells[0].Value == null) return;
NewItemForm newItemForm = new (1, row, tableName);
newItemForm.ShowDialog();
FillDataGrid(tableName);
}
}
}