-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmFunctions.cs
222 lines (204 loc) · 7 KB
/
frmFunctions.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
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
using RegnalUDB.Controllers;
using RegnalUDB.Entity_Framework;
using RegnalUDB.Models;
using RegnalUDB.Utils;
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;
using static RegnalUDB.Utils.FormValidators;
namespace RegnalUDB
{
public partial class frmFunctions : Form
{
private static FunctionController functionController = new FunctionController();
private List<Funcione> functions = new List<Funcione>();
private Funcione selectedFunction = null;
// data for custom DataGridView
private int[] columnsToChange = { 0, 1, 2 };
private int[] columnsToHide = { 3, 4, 5 };
private string[] titlesforColumns = { "ID", "Nombre", "Activo" };
public frmFunctions()
{
InitializeComponent();
}
private void fillSelectedData(Funcione currentFunction)
{
txtName.Text = currentFunction.nombre;
chbStatus.Checked = currentFunction.baja;
}
private void loadTable()
{
Operation<Funcione> getFunctionsOperation = functionController.getRecords();
if (getFunctionsOperation.State)
{
functions = getFunctionsOperation.Data;
dgvFunctions.DataSource = functions;
FormUtils.changeTitlesForDgv(titlesforColumns, columnsToChange, dgvFunctions);
FormUtils.hideColumnsForDgv(columnsToHide, dgvFunctions);
return;
}
MessageBox.Show("Error al cargar datos de funciones");
}
private void saveData()
{
Funcione tempFunc = new Funcione
{
nombre = txtName.Text,
baja = chbStatus.Checked,
idNivel = 0
};
Operation<Funcione> operation = functionController.addRecord(tempFunc);
if (operation.State)
{
MessageBox.Show("Función registrada con éxito",
"OPERACIÓN EXITOSA", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadTable();
cleanForm();
}
else
{
MessageBox.Show("Ocurrió un error inesperado al registrar la función: " + operation.Error,
"ERROR AL INGRESAR DATOS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void updateData(Funcione currentFunction)
{
Operation<Funcione> operation = functionController.updateRecord(currentFunction);
if (operation.State)
{
MessageBox.Show("Función actualizada con éxito",
"OPERACIÓN EXITOSA", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadTable();
cleanForm();
}
else
{
MessageBox.Show("Ocurrió un error inesperado al actualizar la función: " + operation.Error,
"ERROR AL ACTUALIZAR DATOS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void filterData()
{
string value = txtSearch.Text;
if (!this.Equals(""))
{
List<Funcione> tempFunctions = new List<Funcione>();
foreach (Funcione f in functions)
{
if (f.nombre.Contains(value))
{
tempFunctions.Add(f);
}
}
dgvFunctions.DataSource = tempFunctions;
}
else
{
loadTable();
}
}
private ToValidate[] getValidators()
{
ToValidate[] validators = {
new ToValidate(txtName, new ControlValidator[] { FormValidators.hasText },
new string[] { "Ingresa un nombre para la función" })
};
return validators;
}
private void cleanForm()
{
txtName.Clear();
btnSaveModify.Text = "Guardar";
chbStatus.Checked = true;
selectedFunction = null;
errorProvider.Clear();
}
private void frmFunctions_Load(object sender, EventArgs e)
{
try
{
loadTable();
}
catch (Exception ex)
{
FormUtils.defaultErrorMessage(ex);
}
}
private void btnSaveModify_Click(object sender, EventArgs e)
{
try
{
this.errorProvider.Clear();
List<ControlErrorProvider> errorProvider = FormValidators.validFormTest(getValidators());
bool isValid = errorProvider == null;
if (isValid)
{
if (selectedFunction == null)
{
saveData();
}
else
{
selectedFunction.nombre = txtName.Text;
selectedFunction.baja = chbStatus.Checked;
updateData(selectedFunction);
}
}
else
{
MessageBox.Show("Algunos datos proporcionados son inválidos. Pase el puntero sobre los íconos de error para ver los detalles de cada campo.", "ERROR DE VALIDACIÓN", MessageBoxButtons.OK, MessageBoxIcon.Warning);
foreach (ControlErrorProvider error in errorProvider)
{
this.errorProvider.SetError(error.ControlName, error.ErrorMessage);
}
}
}
catch (Exception ex)
{
FormUtils.defaultErrorMessage(ex);
}
}
private void btnNewClean_Click(object sender, EventArgs e)
{
try {
cleanForm();
}
catch (Exception ex)
{
FormUtils.defaultErrorMessage(ex);
}
}
private void dgvFunctions_CellClick(object sender, DataGridViewCellEventArgs e)
{
try {
int index = e.RowIndex;
if (index >= 0)
{
selectedFunction = functions[index];
btnSaveModify.Text = "Modificar";
fillSelectedData(selectedFunction);
}
}
catch (Exception ex)
{
FormUtils.defaultErrorMessage(ex);
}
}
private void txtSearch_KeyUp(object sender, KeyEventArgs e)
{
try {
filterData();
}
catch (Exception ex)
{
FormUtils.defaultErrorMessage(ex);
}
}
}
}