Delete Rows from DataGridView using Checkbox with ADO.NET

Step 1

Create database and table named Customers using definition as shown in the below figure



Step 2

Now, using Visual Studio, we create application windows Forms named CheckBoxGridView, then click on OK. You can see the following figure.



Step 3

At this stage, we need to add connection to database. You shouldbe selecting Server Name, then the Database Name (in this case: DbGridView) that will be used in our application.

*DbGridView

Step 4

Now, Add DataGridView and button controls to the form by selecting them from Toolbox as shown in the following figure,


Step 5

Adding source code for DataGridView.

  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.ComponentModel;  
  4. usingSystem.Data;  
  5. usingSystem.Data.SqlClient;  
  6. usingSystem.Drawing;  
  7. usingSystem.Linq;  
  8. usingSystem.Text;  
  9. usingSystem.Threading.Tasks;  
  10. usingSystem.Windows.Forms;  
  11. namespace CheckBoxGridView  
  12. {  
  13.     publicpartialclassForm1: Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         privateSqlConnectionconx = newSqlConnection("Data Source=.;Initial Catalog=DbGridView;Integrated Security=True"); // Connection String  
  20.         privateSqlDataAdapteradp = null;  
  21.         privateDataSet ds = newDataSet();  
  22.         // loading data from "Customers" Table to DataGridView  
  23.         privatevoidLoadData()  
  24.         {  
  25.             AddCheckBoxForDataGridView();  
  26.             adp = newSqlDataAdapter("SELECT * FROM Customers", conx);  
  27.             adp.Fill(ds);  
  28.             dataGridView1.DataSource = ds.Tables[0];  
  29.         }  
  30.         // AddCheckBoxForDataGridView Function allow to add checkbox in DataGridView  
  31.         privatevoidAddCheckBoxForDataGridView()  
  32.         {  
  33.             DataGridViewCheckBoxColumn col = newDataGridViewCheckBoxColumn()  
  34.             {  
  35.                 Name = "Select Rows to be deleted"  
  36.             };  
  37.             dataGridView1.Columns.Add(col);  
  38.         }  
  39.         privatevoid Form1_Load(object sender, EventArgs e)  
  40.         {  
  41.             LoadData();  
  42.         }  
  43.         privatevoidDeleteRowFromGrid(int id)  
  44.         {  
  45.             try  
  46.             {  
  47.                 conx.Open(); //Open Connection  
  48.                 SqlCommandcmd = newSqlCommand("DELETE FROM Customers WHERE CustomerID = '" + id + "'", conx); //Here, we specify query with {Id} parameter which allow us to delete rows from Db.  
  49.                 int Result = cmd.ExecuteNonQuery(); // Execute Query for deleting all rows selected from DataGridView  
  50.                 conx.Close(); // Close Connection  
  51.             }  
  52.             catch (Exception ex)  
  53.             {  
  54.                 MessageBox.Show(ex.Message);  
  55.             }  
  56.             finally  
  57.             {  
  58.                 conx.Close();  
  59.             }  
  60.         }  
  61.         //Delete Button  
  62.         privatevoid button1_Click(object sender, EventArgs e)  
  63.         {  
  64.             DataGridViewRow row = newDataGridViewRow();  
  65.             for (inti = 0; i < dataGridView1.Rows.Count; i++)  
  66.             {  
  67.                 row = dataGridView1.Rows[i];  
  68.                 if (Convert.ToBoolean(row.Cells[0].Value))  
  69.                 {  
  70.                     int id = Convert.ToInt16(row.Cells[1].Value);  
  71.                     DeleteRowFromGrid(id);  
  72.                     dataGridView1.Rows.Remove(row);  
  73.                     i--;  
  74.                 }  
  75.             }  
  76.         }  
  77.     }  
  78. }  
Step 6

Run Application.