delete multiple rows in datagridview using check box



This is A C# Practice code:
        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 System.Data.SqlClient;

        namespace dataentry
        {
            public partial class DELROWS : Form
            {
                public DELROWS()
                {
                    InitializeComponent();
                }
                SqlConnection CON = new SqlConnection("Data Source=.;Initial Catalog=DATAENTRY;Integrated Security=True;Pooling=False");
                private void button12_Click(object sender, EventArgs e)
                {
                    SqlDataAdapter asdf = new SqlDataAdapter("select * from Register",CON);
                    DataTable ss = new DataTable();
                    asdf.Fill(ss);
                    dataGridView1.Rows.Clear();
                    foreach (DataRow data in ss.Rows)
                    {
                        int aa = dataGridView1.Rows.Add();
                        dataGridView1.Rows[aa].Cells[0].Value = "true";
                        dataGridView1.Rows[aa].Cells[1].Value = data["id"].ToString();
                        dataGridView1.Rows[aa].Cells[2].Value = data["name"].ToString();
                        dataGridView1.Rows[aa].Cells[3].Value = data["gender"].ToString();
                        dataGridView1.Rows[aa].Cells[4].Value = data["age"].ToString();
                        dataGridView1.Rows[aa].Cells[5].Value = data["salary"].ToString();
                        dataGridView1.Rows[aa].Cells[6].Value = data["tax"].ToString();
                        dataGridView1.Rows[aa].Cells[7].Value = data["status"].ToString();
                    }
                    label2.Text = "label";

                }

                private void button1_Click(object sender, EventArgs e)
                {
                    foreach (DataGridViewRow dataRow in dataGridView1.Rows)
                    {
                        if (bool.Parse(dataRow.Cells[0].Value.ToString()))
                        {
                            label2.Text = dataRow.Cells[0].RowIndex.ToString();
                            SqlDataAdapter abc = new SqlDataAdapter("delete from register where id='" + dataRow.Cells[1].Value.ToString() + "' ", CON);
                            DataTable bb = new DataTable();
                            abc.Fill(bb);
                            
                        }
                    }
                    MessageBox.Show("Successfully Deleted Selected Rows...!!");
                }
            }
        }

Video For Result:



Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Facebook Comment