.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Deleting multiple row by selecting checkbox on Data Gridview

Posted By:satyapriyanayak       Posted Date: April 24, 2014    Points: 40    Category: DataGrid    URL: http://www.dotnetspark.com  

In this Code Snippets we will know how to delete multiple row by selecting checkbox on Data Gridview.
 

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.OleDb;
namespace Delete_row_checkbox__DataGridview
{
    public partial class Form1 : Form
    {
        OleDbDataAdapter oledbda;
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        string str;

        OleDbCommand com;
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            int i = 0;
            List<int> ChkedRow = new List<int>();

            for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
                {
                    ChkedRow.Add(i);
                }
            }

            if (ChkedRow.Count == 0)
            {
                MessageBox.Show("Select one checkbox");
                return;
            }


            foreach (int j in ChkedRow)
            {
                str = "Delete from test where Did='" + dataGridView1.Rows[j].Cells["Did"].Value + "'";

                try
                {
                    using (OleDbConnection con = new OleDbConnection(ConnectionString))
                    {
                        using (com = new OleDbCommand(str, con))
                        {
                            con.Open();
                            com.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            MessageBox.Show("Are you sure you want to delete this Record?", "Confirm deletion", MessageBoxButtons.OK);

            bindgrid();
        }
        private void bindgrid()
        {
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Columns.Clear();
            DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn();
            col1.Name = "chkcol";
            col1.HeaderText = "CheckBox";
            dataGridView1.Columns.Add(col1);
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from test ";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            DataSet ds = new DataSet();
            oledbda.Fill(ds, "test");
            dataGridView1.DataMember = "test";
            dataGridView1.DataSource = ds;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bindgrid();
        }
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend