Delete Multiple Records From A DataGridView Using CheckBox In Windows Form

In my previous article, I clearly discussed how to delete records from a DataGridView using ContextMenuStrip. In that we deleted record one by one. Here's the article link:
 Here in this article I decided to delete multiple records using Checkbox. So for this in my project I have taken a GridView as shown below.
 


Just click the GridView Task.
 


After that to add columns by mapping my database table click Add Columns.
 


So In this way you can add column in the DataGridView. Now to add Checkbox inside the GridView choose GridViewCheckbox column as follows.
 


It will add Checkbox in the Gridview for a column. After adding all these it will appear as follows.
 


Now to disable the default empty row just check the following Checkbox as shown below.
 


Now to bind data in the DataGridView write the following code.
  1. public partial class Form1: Form  
  2. {  
  3.     SqlConnection con = new SqlConnection("Data Source=DEBENDRA;Initial Catalog=Students;User ID=sa;Password=123");  
  4.     int Index = 0;  
  5.     SqlDataAdapter da;  
  6.     public Form1()  
  7.     {  
  8.         InitializeComponent();  
  9.     }  
  10.     private void Form1_Load(object sender, EventArgs e)  
  11.     {  
  12.         filldata();  
  13.     }  
  14.     public void filldata()  
  15.     {  
  16.         da = new SqlDataAdapter("select * from tbl_studentdetails", con);  
  17.         DataTable dt = new DataTable();  
  18.         da.Fill(dt);  
  19.         dataGridView1.AutoGenerateColumns = false;  
  20.         foreach(DataRow row in dt.Rows)  
  21.         {  
  22.             int i = dataGridView1.Rows.Add();  
  23.             dataGridView1.Rows[i].Cells[0].Value = row["RollNo"].ToString();  
  24.             dataGridView1.Rows[i].Cells[1].Value = row["Name"].ToString();  
  25.             dataGridView1.Rows[i].Cells[2].Value = row["State"].ToString();  
  26.             dataGridView1.Rows[i].Cells[3].Value = row["Mark"].ToString();  
  27.         }  
  28.     }  

Now it will show me the data like this. And one more thing add a Delete button just below the form.
 


Now just make the ReadOnly false to the Checkbox. Neither it will allow you to click on them.



Now write the following code to delete the selected item from the Gridview. It mainly consists of 2 steps:
  • Store all the checked row Id into a list.
  • Then by using foreach loop delete the record whose id is stored in the list.
Here is my coding part:
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     List < string > selectedItem = new List < string > ();  
  4.     DataGridViewRow drow = new DataGridViewRow();  
  5.     for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)  
  6.     {  
  7.         drow = dataGridView1.Rows[i];  
  8.         if (Convert.ToBoolean(drow.Cells[4].Value) == true//checking if  checked or not.  
  9.         {  
  10.             string id = drow.Cells[0].Value.ToString();  
  11.             selectedItem.Add(id); //If checked adding it to the list  
  12.         }  
  13.     }  
  14.     con.Open();  
  15.     foreach(string s in selectedItem) //using foreach loop to delete the records stored in list.  
  16.     {  
  17.         SqlCommand cmd = new SqlCommand("delete from tbl_studentdetails where RollNo='" + s + "'", con);  
  18.         int result = cmd.ExecuteNonQuery();  
  19.     }  
  20.     con.Close();  
  21.     dataGridView1.Rows.Clear();  
  22.     filldata();  
So here is my result. I am selecting the above rows.



Now when I click the delete button it will delete the selected row and show the remaining records as follows.



So in this way we can delete multiple records using Checkbox inside GridView.