9 de marzo de 2012

DataGridViewCheckBoxColumn C#




using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Data.SqlClient;
Public Class Form1
{
       SqlCommand cmd;
       string cadena = ConfigurationManager.ConnectionStrings("cnn").ConnectionString.ToString;
       SqlConnection cnn;
       private void Form1_Load(System.Object sender, System.EventArgs e)
       {
             cargardatos();
       }
       //Cargar el DGV y crear dinamicamente la columna checkbox
       private void cargardatos()
       {
             DataGridView1.Columns.Clear();
             DataGridViewCheckBoxColumn obj = new DataGridViewCheckBoxColumn();
             int col = 0;
             var _with1 = obj;
             obj.HeaderText = "Eliminar";
             obj.Name = "Eliminar";
             obj.Width = 50;

             var _with2 = DataGridView1;
             DataGridView1.Columns.Add(obj);
             DataGridView1.Columns(DataGridView1.Columns.Count - 1).DisplayIndex = 0;
             DataGridView1.ReadOnly = false;
             DataGridView1.DataSource = null;

             try {
                    using (cnn == new SqlConnection(cadena)) {
                           cnn.Open();
                           SqlDataAdapter sql = new SqlDataAdapter("select * from img", cnn);
                           DataSet da = new DataSet();
                           sql.Fill(da);
                           DataGridView1.DataSource = da.Tables[0];
                    }
             } catch (Exception ex) {
                    Interaction.MsgBox(ex.Message.ToString());
             }

             for (col = 1; col <= DataGridView1.Columns.Count - 1; col++) {
                    DataGridView1.Columns(col).ReadOnly = true;
             }
       }

       //aqui se quitan los check a todos aquellos checkbox que estes checkeados
       private void Button1_Click(System.Object sender, System.EventArgs e)
       {
             for (int i = 0; i <= this.DataGridView1.Rows.Count - 1; i++) {
                    if (DataGridView1.Rows(i).Cells(0).Value == true) {
                           DataGridView1.Rows(i).Cells(0).Value = false;
                    } else {
                           DataGridView1.Rows(i).Cells(0).Value = true;
                    }
             }
       }

       //aqui simplemente se quita el check sobre el checkbox clickeado en caso ya haya estado checkeado
       private void DataGridView1_CellContentClick(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
       {
             if (e.RowIndex < 0 | !(e.ColumnIndex == 0))
                    return;
             if (Convert.ToBoolean(DataGridView1.Rows(e.RowIndex).Cells(0).Value)) {
                    DataGridView1.Rows(e.RowIndex).Cells(0).Value = false;
             } else {
                    DataGridView1.Rows(e.RowIndex).Cells(0).Value = true;
             }
       }

       //Aqui verifica que filas tienen el check activado para luego enviar el id correspondiente a la funcion eliminar
       private void Button2_Click(System.Object sender, System.EventArgs e)
       {
             foreach (DataGridViewRow fila in this.DataGridView1.Rows) {
                    if (Convert.ToBoolean(fila.Cells("Eliminar").Value)) {
                           eliminar(fila.Cells("id").Value.ToString());
                    }
             }
       }

       //Aqui se eliminan los registros con check activado segun el id
       private void eliminar(string id)
       {
             try {
                    using (cnn == new SqlConnection(cadena)) {
                           cnn.Open();
                           SqlCommand p = new SqlCommand("eliminar", cnn);
                           p.CommandType = CommandType.StoredProcedure;
                           p.Parameters.Add("@codigo", SqlDbType.Int).Value = id;
                           p.ExecuteNonQuery();
                           MessageBox.Show("registro eliminado satisfactoriamente");
                           cargardatos();
                    }

             } catch (Exception ex) {
                    Interaction.MsgBox(ex.Message.ToString());
             }

       }
    Public Form1()
       {
             Load += Form1_Load;
       }

}


viernes, marzo 09, 2012