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.
- public partial class Form1: Form
- {
- SqlConnection con = new SqlConnection("Data Source=DEBENDRA;Initial Catalog=Students;User ID=sa;Password=123");
- int Index = 0;
- SqlDataAdapter da;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- filldata();
- }
- public void filldata()
- {
- da = new SqlDataAdapter("select * from tbl_studentdetails", con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- dataGridView1.AutoGenerateColumns = false;
- foreach(DataRow row in dt.Rows)
- {
- int i = dataGridView1.Rows.Add();
- dataGridView1.Rows[i].Cells[0].Value = row["RollNo"].ToString();
- dataGridView1.Rows[i].Cells[1].Value = row["Name"].ToString();
- dataGridView1.Rows[i].Cells[2].Value = row["State"].ToString();
- dataGridView1.Rows[i].Cells[3].Value = row["Mark"].ToString();
- }
- }
- }
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:
- private void button1_Click(object sender, EventArgs e)
- {
- List < string > selectedItem = new List < string > ();
- DataGridViewRow drow = new DataGridViewRow();
- for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
- {
- drow = dataGridView1.Rows[i];
- if (Convert.ToBoolean(drow.Cells[4].Value) == true)
- {
- string id = drow.Cells[0].Value.ToString();
- selectedItem.Add(id);
- }
- }
- con.Open();
- foreach(string s in selectedItem)
- {
- SqlCommand cmd = new SqlCommand("delete from tbl_studentdetails where RollNo='" + s + "'", con);
- int result = cmd.ExecuteNonQuery();
- }
- con.Close();
- dataGridView1.Rows.Clear();
- 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.