programacion ii, clase 11-05-2015
DESCRIPTION
Programacion II, Clase 11-05-2015TRANSCRIPT
-
CENTRO REGIONAL DE USULUTN, FACULTAD DE CIENCIA Y TECNOLOGA
PROGRAMACIN II, TC. EN ING. EN SISTEMAS Y REDES LIC. MAURICIO ALBERTO TURCIOS BENAVIDES
Clase 11 de abril de 2015
Unidad 05 Acceso a Base de Datos 5.1 Comandos de SQL
5.2 Acceso a la base de datos 5.3 ADO.NET
5.4 Vincular controles con la base de datos
Creamos un nuevo proyecto:
Diseo Form1:
Controles a utilizar:
1 Texbox
1 Label
1 PictureBox
1 DataGridView (Agregar una nueva columna llamada Eliminar con el control CheckBoxColumn)
-
CENTRO REGIONAL DE USULUTN, FACULTAD DE CIENCIA Y TECNOLOGA
PROGRAMACIN II, TC. EN ING. EN SISTEMAS Y REDES LIC. MAURICIO ALBERTO TURCIOS BENAVIDES
Clase 11 de abril de 2015
-
CENTRO REGIONAL DE USULUTN, FACULTAD DE CIENCIA Y TECNOLOGA
PROGRAMACIN II, TC. EN ING. EN SISTEMAS Y REDES LIC. MAURICIO ALBERTO TURCIOS BENAVIDES
Clase 11 de abril de 2015
-
CENTRO REGIONAL DE USULUTN, FACULTAD DE CIENCIA Y TECNOLOGA
PROGRAMACIN II, TC. EN ING. EN SISTEMAS Y REDES LIC. MAURICIO ALBERTO TURCIOS BENAVIDES
Clase 11 de abril de 2015
Cdigo de la aplicacin: 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 System.Data.SqlClient;//libreria para trabajar con sql
namespace conexion_csharp { public partial class Form1 : Form {
//declaramos la cadena de conexion string cadenaconexion = @"Data Source=PROPIETARIO\SQL;Initial Catalog=inventario;Integrated Security=True";
public Form1() { InitializeComponent(); }
public void BuscarGrid() { SqlConnection con = new SqlConnection(cadenaconexion); con.Open(); string val = textBox1.Text; SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblproductos where id_prod like '%" + val + "%'", con); DataSet ds = new DataSet(); da.Fill(ds, "tblproductos"); dataGridView1.DataSource = ds.Tables[0]; for (int i = 0; i < dataGridView1.Columns.Count; i++) { dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; }//con este for impiden ordenar el grid dando clic en las columnas }
public void actualizar_grid() { SqlConnection con = new SqlConnection(cadenaconexion); con.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblproductos", con); DataSet ds = new DataSet(); da.Fill(ds, "tblproductos"); dataGridView1.DataSource = ds.Tables[0]; for (int i = 0; i < dataGridView1.Columns.Count; i++) { dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
-
CENTRO REGIONAL DE USULUTN, FACULTAD DE CIENCIA Y TECNOLOGA
PROGRAMACIN II, TC. EN ING. EN SISTEMAS Y REDES LIC. MAURICIO ALBERTO TURCIOS BENAVIDES
Clase 11 de abril de 2015
}//con este for impiden ordenar el grid dando clic en las columnas }
private void SetFontAndColorsGrid() { this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 9); this.dataGridView1.DefaultCellStyle.ForeColor = Color.Blue; this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige; this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow; this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black; }
private void Form1_Load(object sender, EventArgs e) {
actualizar_grid(); SetFontAndColorsGrid();
} private void textBox1_TextChanged(object sender, EventArgs e) {
BuscarGrid(); }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (this.dataGridView1.Columns[e.ColumnIndex].Name.Equals("Eliminar")) { DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; DataGridViewCheckBoxCell cellSelection = row.Cells["Eliminar"] as DataGridViewCheckBoxCell; if (Convert.ToBoolean(cellSelection.Value)) { string mensaje = string.Format("Se ha seleccionado para eliminar: \nId: '{0}', \nNombre: '{1}'", row.Cells["id_prod"].Value, row.Cells["descrip"].Value); MessageBox.Show(mensaje, "Eliminando...", MessageBoxButtons.OK, MessageBoxIcon.Information); } SqlConnection sqlConnection1 = new SqlConnection(cadenaconexion); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; string id = ((row.Cells["id_prod"].Value).ToString()).Trim(); cmd.CommandText = "Delete FROM tblproductos where id_prod= '" + id + "'"; cmd.Connection = sqlConnection1; sqlConnection1.Open(); cmd.ExecuteNonQuery(); sqlConnection1.Close(); actualizar_grid(); }
}
-
CENTRO REGIONAL DE USULUTN, FACULTAD DE CIENCIA Y TECNOLOGA
PROGRAMACIN II, TC. EN ING. EN SISTEMAS Y REDES LIC. MAURICIO ALBERTO TURCIOS BENAVIDES
Clase 11 de abril de 2015
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) {
if (dataGridView1.IsCurrentCellDirty) { dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); }
} } }