In this article I will explain how to delete (remove) multiple selected rows with confirmation from DataGridView based on CheckBox selection in Windows Forms (WinForms) Application using C# and VB.Net.
User has to select (check) the CheckBox and click the Delete button after which he will see a Confirmation MessageBox and when he clicks Yes button, the selected rows will be deleted.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Delete multiple rows from DataGridView based on CheckBox selection in Windows Forms Application using C# and VB.Net
 
I have already inserted few records in the table.
Delete multiple rows from DataGridView based on CheckBox selection in Windows Forms Application using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
 
 
Form Design
You will need to add a DataGridView control and a Button to the Windows Form.
Delete multiple rows from DataGridView based on CheckBox selection in Windows Forms Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Linq;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Linq
Imports System.Data.SqlClient
 
 
Populating the DataGridView from Database
The DataGridView is populated inside the Form Load event. First the DataGridView configuration is done by adding columns for CheckBox and the data.
Then the BindGrid method is called which populates records from the Customers table and populates the DataGridView control.
C#
private const string ConnectionString = @"Data Source=.\SQL2008R2;Initial Catalog=Samples;Integrated Security = true";
public Form1()
{
    InitializeComponent();
 
    //Set Columns Count
    dataGridView1.ColumnCount = 3;
 
    //Hide the last blank line
    dataGridView1.AllowUserToAddRows = false;
 
    //Add Columns
    dataGridView1.Columns[0].Name = "CustomerId";
    dataGridView1.Columns[0].HeaderText = "CustomerId Id";
    dataGridView1.Columns[0].DataPropertyName = "CustomerId";
    dataGridView1.Columns[0].Width = 100;
 
    dataGridView1.Columns[1].HeaderText = "Name";
    dataGridView1.Columns[1].Name = "Name";
    dataGridView1.Columns[1].DataPropertyName = "Name";
    dataGridView1.Columns[1].Width = 100;
 
    dataGridView1.Columns[2].Name = "Country";
    dataGridView1.Columns[2].HeaderText = "Country";
    dataGridView1.Columns[2].DataPropertyName = "Country";
    dataGridView1.Columns[2].Width = 100;
 
    DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
    checkBoxColumn.HeaderText = "";
    checkBoxColumn.Width = 30;
    checkBoxColumn.Name = "checkBoxColumn";
    dataGridView1.Columns.Insert(0, checkBoxColumn);
 
    this.BindGrid();
}
 
private void BindGrid()
{
    dataGridView1.DataSource = null;
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }
    }
}
 
VB.Net
Private Const ConnectionString As String = "Data Source=.\SQL2008R2;Initial Catalog=Samples;Integrated Security = true"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Set Columns Count
    dataGridView1.ColumnCount = 3
 
    'Hide the last blank line
    dataGridView1.AllowUserToAddRows = False
 
    'Add Columns
    dataGridView1.Columns(0).Name = "CustomerId"
    dataGridView1.Columns(0).HeaderText = "CustomerId Id"
    dataGridView1.Columns(0).DataPropertyName = "CustomerId"
    dataGridView1.Columns(0).Width = 100
 
    dataGridView1.Columns(1).HeaderText = "Name"
    dataGridView1.Columns(1).Name = "Name"
    dataGridView1.Columns(1).DataPropertyName = "Name"
    dataGridView1.Columns(1).Width = 100
 
    dataGridView1.Columns(2).Name = "Country"
    dataGridView1.Columns(2).HeaderText = "Country"
    dataGridView1.Columns(2).DataPropertyName = "Country"
    dataGridView1.Columns(2).Width = 100
 
    Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
    checkBoxColumn.HeaderText = ""
    checkBoxColumn.Width = 30
    checkBoxColumn.Name = "checkBoxColumn"
    dataGridView1.Columns.Insert(0, checkBoxColumn)
 
    Me.BindGrid()
End Sub
 
Private Sub BindGrid()
    dataGridView1.DataSource = Nothing
    Using con As New SqlConnection(ConnectionString)
        Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
            cmd.CommandType = CommandType.Text
            Using sda As New SqlDataAdapter(cmd)
                Using dt As New DataTable()
                    sda.Fill(dt)
                    dataGridView1.DataSource = dt
                End Using
            End Using
        End Using
    End Using
End Sub
 
Delete multiple rows from DataGridView based on CheckBox selection in Windows Forms Application using C# and VB.Net
 
 
Deleting multiple rows from DataGridView control on Button click
When the Delete button is clicked the following event handler is executed. It first gets the checked (selected) rows from the DataGridView using LINQ query and then displays a confirmation Message Box.
If the User clicks Yes button then a loop is executed over the selected (checked) rows and one by one records are deleted from the database table using the CustomerId field.
C#
private void btnDelete_Click(object sender, EventArgs e)
{
    List<DataGridViewRow> selectedRows = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                                            where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
                                            select row).ToList();
    if (MessageBox.Show(string.Format("Do you want to delete {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        foreach (DataGridViewRow row in selectedRows)
        {
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId", con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@CustomerId", row.Cells["CustomerId"].Value);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
 
        this.BindGrid();
    }
}
 
VB.Net
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    Dim selectedRows As List(Of DataGridViewRow) = (From row In dataGridView1.Rows.Cast(Of DataGridViewRow)() _
                                                    Where Convert.ToBoolean(row.Cells("checkBoxColumn").Value) = True).ToList()
    If MessageBox.Show(String.Format("Do you want to delete {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
        For Each row As DataGridViewRow In selectedRows
            Using con As New SqlConnection(ConnectionString)
                Using cmd As New SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId", con)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@CustomerId", row.Cells("CustomerId").Value)
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()
                End Using
            End Using
        Next
 
        Me.BindGrid()
    End If
End Sub
 
Delete multiple rows from DataGridView based on CheckBox selection in Windows Forms Application using C# and VB.Net
 
 
Downloads