Skip to content
</> Mücahit TopalA developer's notebook

Blog/C#

Multiple Delete with CheckBoxes in C# (ListView)

Mücahit Topal 2 min read 8,975 views TREN

Let's go through a code example you can use at times when you need to select more than one checkbox and have an operation performed. Here I worked with MySql and had the checkboxes on a ListView selected, then carried out a delete operation. You can adapt it and use it however you like.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace deneme
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            
            InitializeComponent();

            this.listView1.MultiSelect = true;
            this.listView1.CheckBoxes = true;
            ColumnHeader.Text = "CheckBox Control";
        }
        public MySqlConnection mysqlbaglan = new MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd='';Encrypt=false;AllowUserVariables=True;UseCompression=True;");// by adding charset we have also fixed the Turkish character problem.

        public void  baglantı_kontrol()
        {
            try
            {
                mysqlbaglan.Open();
                if (mysqlbaglan.State != ConnectionState.Closed)
                {
                    MessageBox.Show("Connection Successful ");
                }
                else
                {
                    MessageBox.Show("Unfortunately the Connection Could Not Be Established...!");
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("Error! " + err.Message, "An Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void listele()
        {
            MySqlCommand komut = new MySqlCommand("Select * from checkbox", mysqlbaglan);
            MySqlDataReader oku = komut.ExecuteReader();
            while (oku.Read())
            {
                ListViewItem listele = new ListViewItem();
                listele.Text = oku["id"].ToString();
                listele.SubItems.Add(oku["id"].ToString());

                listView1.Items.Add(listele);

            }
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            baglantı_kontrol(); // I check my database connection

            listele(); // I fill the ListView content

            mysqlbaglan.Close(); //I close the connection

        }

        private void button1_Click(object sender, EventArgs e)
        {

            mysqlbaglan.Open();

                for (int i = 0; i < listView1.Items.Count; i++ )
                    if (listView1.Items[i].Checked) // I specify the operation to perform for each checked checkbox, up to the value of the variable i
                          {
                              //the operations to perform go here  
                             string sorgu1 = string.Format(" delete from checkbox where id ='{0}'", listView1.Items[i].SubItems[1].Text);
                             MySqlCommand sil = new MySqlCommand(sorgu1, mysqlbaglan);
                             sil.ExecuteNonQuery();
                             
                          }
                          listView1.Items.Clear(); // I clear the ListView items
                          listele(); // I fill the items that were not deleted back into the ListView (in effect refreshing it)
            mysqlbaglan.Close();
        }
    }
}

That's all for our code, friends. Good luck with your work.

,,,,

Share X Facebook LinkedIn WhatsApp

More from this category

Comments 0

No comments yet.

Leave a comment

Comments appear after approval.