-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListadoClientes.cs
199 lines (171 loc) 路 6.94 KB
/
ListadoClientes.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
锘縰sing iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;
namespace SmartPhone7
{
public partial class ListadoClientes : Form
{
public ListadoClientes()
{
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 MostrarClientes()
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT * FROM Clientes";
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 clientes: " + ex.Message);
}
DataGridViewClientes.DataSource = dtUsuarios;
lblTotal.Text = DataGridViewClientes.RowCount.ToString();
}
}
private void BuscarClientes(string busqueda)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT * FROM Clientes WHERE NombreCliente LIKE @busqueda OR Telefono LIKE @busqueda OR MarcaModelo LIKE @busqueda OR Diagnostico LIKE @busqueda";
DataTable dt = new DataTable();
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
comando.Parameters.AddWithValue("@busqueda", "%" + busqueda + "%");
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
adaptador.Fill(dt);
adaptador.Dispose();
conexion.Close();
}
catch (Exception ex)
{
throw new Exception("Error al buscar los Clientes: " + ex.Message);
}
DataGridViewClientes.DataSource = dt;
lblTotal.Text = DataGridViewClientes.RowCount.ToString();
}
}
private void EliminarClientes(int id)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "DELETE FROM Clientes 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 guna2Button1_Click(object sender, EventArgs e)
{
id = 0;
Clientes formularioModal = new Clientes();
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
formularioModal.ShowDialog(formularioDeFondo);
formularioModal.Dispose();
formularioDeFondo.Dispose();
MostrarClientes();
}
private void ListadoClientes_Load(object sender, EventArgs e)
{
MostrarClientes();
}
private void btnEliminar_Click(object sender, EventArgs e)
{
if (DataGridViewClientes.SelectedRows.Count > 0)
{
DialogResult result = MessageBox.Show("Estas Seguro que quieres eliminar este cliente, esto significara que no esta informacion no volvera a estar disponible", "Eliminar cliente" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
if (result == DialogResult.OK)
{
EliminarClientes(id);
MostrarClientes();
}
}
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 DataGridViewClientes_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = DataGridViewClientes.Rows[e.RowIndex];
id = Convert.ToInt16(row.Cells[0].Value);
}
}
private void guna2Button4_Click(object sender, EventArgs e)
{
if (id != 0)
{
Clientes.IdCliente = id;
Clientes formularioModal = new Clientes();
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
formularioModal.ShowDialog(formularioDeFondo);
formularioModal.Dispose();
formularioDeFondo.Dispose();
MostrarClientes();
}
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 txtBuscar_TextChanged_1(object sender, EventArgs e)
{
BuscarClientes(txtBuscar.Text);
if (DataGridViewClientes.Rows.Count < 1)
{
panelSearch.Visible = true;
}
else
{
panelSearch.Visible = false;
}
}
private void guna2Button3_Click(object sender, EventArgs e)
{
UtilidadesFormularios.ExportToPDF(DataGridViewClientes, "Listado de todo los clientes");
}
}
}