-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListadoProducto.cs
200 lines (172 loc) · 6.97 KB
/
ListadoProducto.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SmartPhone7
{
public partial class ListadoProducto : Form
{
public ListadoProducto()
{
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 MostrarProducto()
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT * FROM Producto";
DataTable dt = new DataTable();
try
{
conexion.Open();
SqlCommand comando = new SqlCommand(consulta, conexion);
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
adaptador.Fill(dt);
adaptador.Dispose();
conexion.Close();
}
catch (Exception ex)
{
throw new Exception("Error al mostrar los Producto: " + ex.Message);
}
DataGridViewProductos.DataSource = dt;
lblTotal.Text = DataGridViewProductos.RowCount.ToString();
}
}
private void BuscarProducto(string busqueda)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "SELECT * FROM Producto WHERE NombreProducto LIKE @busqueda OR ModeloMarca LIKE @busqueda OR Codigo LIKE @busqueda OR DescripcionProducto 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 Productos: " + ex.Message);
}
DataGridViewProductos.DataSource = dt;
lblTotal.Text = DataGridViewProductos.RowCount.ToString();
}
}
private void EliminarProducto(int id)
{
using (SqlConnection conexion = new SqlConnection(conexion1.cadenaConexion))
{
string consulta = "DELETE FROM Producto 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 ListadoProducto_Load(object sender, EventArgs e)
{
MostrarProducto();
}
private void btnPDF_Click(object sender, EventArgs e)
{
UtilidadesFormularios.ExportToPDF(DataGridViewProductos, "Listado de todo los productos");
}
private void txtBuscar_TextChanged(object sender, EventArgs e)
{
BuscarProducto(txtBuscar.Text);
if (DataGridViewProductos.Rows.Count < 1)
{
panelSearch.Visible = true;
}
else
{
panelSearch.Visible = false;
}
}
private void DataGridViewProductos_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = DataGridViewProductos.Rows[e.RowIndex];
id = Convert.ToInt16(row.Cells[0].Value);
}
}
private void btnEliminar_Click(object sender, EventArgs e)
{
if (DataGridViewProductos.SelectedRows.Count > 0)
{
DialogResult result = MessageBox.Show("Estas Seguro que quieres eliminar este Producto, esto significara que no esta informacion no volvera a estar disponible", "Eliminar Producto" + id.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
if (result == DialogResult.OK)
{
EliminarProducto(id);
MostrarProducto();
}
}
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 btnEditar_Click(object sender, EventArgs e)
{
if (id != 0)
{
Producto.IdProducto = id;
Producto formularioModal = new Producto();
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
formularioModal.ShowDialog(formularioDeFondo);
formularioModal.Dispose();
formularioDeFondo.Dispose();
MostrarProducto();
}
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 btnNuevo_Click(object sender, EventArgs e)
{
id = 0;
Producto formularioModal = new Producto();
Form formularioDeFondo = UtilidadesFormularios.CrearFormularioDeFondo(Principal.ActiveForm);
UtilidadesFormularios.CentrarFormularioModal(formularioModal, Principal.ActiveForm);
formularioModal.ShowDialog(formularioDeFondo);
formularioModal.Dispose();
formularioDeFondo.Dispose();
MostrarProducto();
}
}
}