-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListadoServicios.cs
208 lines (177 loc) 路 7.27 KB
/
ListadoServicios.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
锘縰sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SmartPhone7
{
public partial class ListadoServicios : Form
{
public ListadoServicios()
{
InitializeComponent();
}
// Cadena de conexi贸n a la base de datos
Conexion conexion1 = new Conexion();
// Objeto de conexi贸n a la base de datos
public int id = 0;
private void MostrarServicio()
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT * FROM Servicios";
DataTable dtUsuarios = new DataTable();
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
adaptador.Fill(dtUsuarios);
adaptador.Dispose();
conexion.Close();
}
catch (Exception ex)
{
throw new Exception("Error al mostrar los Servicios: " + ex.Message);
}
DataGridViewServicio.DataSource = dtUsuarios;
lblTotal.Text = DataGridViewServicio.RowCount.ToString();
}
}
private void BuscarServicio(string busqueda)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT * FROM Servicios WHERE NombreServicio LIKE @busqueda OR Descripcion LIKE @busqueda";
DataTable dtUsuarios = new DataTable();
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
comando.Parameters.AddWithValue("@busqueda", "%" + busqueda + "%");
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
adaptador.Fill(dtUsuarios);
adaptador.Dispose();
conexion.Close();
}
catch (Exception ex)
{
throw new Exception("Error al buscar los Servicios: " + ex.Message);
}
DataGridViewServicio.DataSource = dtUsuarios;
lblTotal.Text = DataGridViewServicio.RowCount.ToString();
}
}
private void EliminarServicio(int id)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "DELETE FROM Servicios WHERE Id = @id";
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
comando.Parameters.AddWithValue("@id", id);
int filasAfectadas = comando.ExecuteNonQuery();
if (filasAfectadas > 0)
{
MessageBox.Show("Registro eliminado correctamente.");
}
else
{
MessageBox.Show("No se encontr贸 ning煤n registro con el Id especificado.");
}
conexion.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error al eliminar el registro: " + ex.Message);
}
}
}
private void txtBuscar_TextChanged_1(object sender, EventArgs e)
{
BuscarServicio(txtBuscar.Text);
if (DataGridViewServicio.Rows.Count < 1)
{
panelSearch.Visible = true;
}
else
{
panelSearch.Visible = false;
}
}
private void btnEditar_Click(object sender, EventArgs e)
{
if (id != 0)
{
Servicio.IdServicio = id;
Servicio formularioModal = new Servicio();
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
formularioModal.ShowDialog(formularioDeFondo);
formularioModal.Dispose();
formularioDeFondo.Dispose();
MostrarServicio();
}
else
{
DialogResult result = MessageBox.Show("Primero tienes que seleccionar la fila que deaseas eliminar para poder realizar la accion", "Error" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
private void guna2Button1_Click_1(object sender, EventArgs e)
{
id = 0;
// Crear el formulario modal
Servicio formularioModal = new Servicio();
// Crear el formulario de fondo
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
// Mostrar el formulario modal centrado en el formulario principal
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
// Mostrar el formulario modal como cuadro de di谩logo
formularioModal.ShowDialog(formularioDeFondo);
// Liberar recursos
formularioModal.Dispose();
formularioDeFondo.Dispose();
//Actualizar datagrid
MostrarServicio();
}
private void guna2Button3_Click_1(object sender, EventArgs e)
{
UtilidadesFormularios.ExportToPDF(DataGridViewServicio, "Listado de todo los servicios");
}
private void btnEliminar_Click(object sender, EventArgs e)
{
if (DataGridViewServicio.SelectedRows.Count > 0)
{
DialogResult result = MessageBox.Show("Estas Seguro que quieres eliminar este Servicio, esto significara que no esta informacion no volvera a estar disponible", "Eliminar Servicio" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
if (result == DialogResult.OK)
{
EliminarServicio(id);
MostrarServicio();
}
}
else
{
DialogResult result = MessageBox.Show("Primero tienes que seleccionar la fila que deaseas eliminar para poder realizar la accion", "Error" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
private void DataGridViewServicio_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0) {
DataGridViewRow row = DataGridViewServicio.Rows[e.RowIndex];
id = Convert.ToInt16(row.Cells[0].Value);
}
}
private void ListadoServicios_Load(object sender, EventArgs e)
{
MostrarServicio();
DataGridViewServicio.ClearSelection();
}
}
}