# Visual Basic > Database Development >  [RESOLVED] Delete record from Database by clicking on a cell in a DataGridView

## The_Hobbyist

My scenario:

- Using SQLite
- There is a DataGridView on my form called "DGV"
- I have a database file (The_Database.db) and that database contains one table.  It looks like this:
ID
FirstName
LastName
Age

1
Sam
Malone
71

2
Alfred
Hitchcock
95



My problem:
I want to be able to click on one of the cells within the ID column of the DGV and have that entire row of data deleted from the database.  The code below sort of works however, it only works when I click on most any other column other than "ID" and even then, its hit and miss.  Sometimes I have to click on a cell 2 or 3 times before the delete occurs.  I don't understand where I am going wrong.

Here is my code:



```
Imports System.Data.SQLite

Public Class frmMain

    Private DatabaseConnection As String = ("Data Source=" & Application.StartupPath & "\The_Database.db;" & "Version=3;New=False;Compressed=True;")
    Private Const ConnectionString As String = "Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security = true"
    Private WithEvents MasterTable_Adpater As New SQLiteDataAdapter("SELECT ID, FirstName, LastName, Age FROM MasterTable", DatabaseConnection)
    Private MasterTable_DataTable As New DataTable

    Dim SQLite_Connect As SQLite.SQLiteConnection
    Dim SQLite_Command As SQLite.SQLiteCommand


    Private Sub DGV_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick

        Dim SelectedRow As DataGridViewRow
        SelectedRow = DGV.Rows(e.RowIndex)
        Dim ID As Integer = SelectedRow.Cells(0).Value

        If MessageBox.Show(String.Format("Do you want to delete ID: {0}", SelectedRow.Cells("ID").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then

            Dim strSQL As String = "DELETE FROM MasterTable WHERE ID='" & ID & "'  "

            SQLite_Connect = New SQLite.SQLiteConnection(DatabaseConnection)
            SQLite_Connect.Open()
            SQLite_Command = New SQLite.SQLiteCommand(strSQL, SQLite_Connect)
            SQLite_Command.ExecuteNonQuery()
            SQLite_Connect.Close()

            MessageBox.Show("Successfully deleted" & ID)

            MasterTable_DataTable = New DataTable
            MasterTable_Adpater.Fill(MasterTable_DataTable)
            Me.DGV.DataSource = MasterTable_DataTable 

        Else

            MessageBox.Show("Delete Canceled")

        End If

    End Sub

End Class
```

Any input is appreciated.  Thank you.

----------


## jmcilhinney

You should be populating a DataTable from the database and binding that to the grid via a BindingSource. You should be handling CellClick, not CellContentClick. You should confirm that the cell clicked was in the appropriate column. You should call RemoveCurrent on the BindingSource to delete the row locally. You should use the same data adapter you used to retrieve the data in order to save the changes by calling Update. Done.

----------


## The_Hobbyist

> You should be populating a DataTable from the database and binding that to the grid via a BindingSource. You should be handling CellClick, nor CellContentClick. You should confirm that the cell clicked was in the appropriate column. You should call RemoveCurrent on the BindingSource to delete the row locally. You should use the same data adapter you used to retrieve the data in order to save the changes by calling Update. Done.


Noted and corrected.  Everything works as it should now.  Thank you.

----------

