Delete Datagridview rows as well as in my access database vb.net

this is my code for loading the data in my database going to my datagrid

Private Sub Records_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connString As String = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Database3.Accdb;"
        Dim MyConn As OleDbConnection
        Dim da As OleDbDataAdapter
        Dim ds As DataSet
        Dim tables As DataTableCollection
        Dim source1 As New BindingSource
        MyConn = New OleDbConnection
        MyConn.ConnectionString = connString
        ds = New DataSet
        tables = ds.Tables
        da = New OleDbDataAdapter("Select * from [userinfo] ORDER BY ID", MyConn) 'Change items to your database name
        da.Fill(ds, "userinfo") 'Change items to your database name
        Dim cb = New OleDbCommandBuilder(da)
        Dim view As New DataView(tables(0))
        source1.DataSource = view
        DataGridView1.DataSource = view
    End Sub

and this is my delete button to delete a selected row in datagrid but the whole table is deleted instead (i know this cause i put it to delete the whole table just to know my sql is working)

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
    Dim connString As String = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Database3.Accdb;"
    Dim MyConn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource
    Dim row As New Integer
    Try
        MyConn = New OleDbConnection
        MyConn.ConnectionString = connString
        ds = New DataSet
        tables = (ds.Tables)
        da = New OleDbDataAdapter("Delete * from [userinfo]", MyConn)
        da.Fill(ds, "userinfo")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

i want to delete a selected row in my datagrid. thank you in advance

Upvotes: 1

Views: 5303

Answers (3)

djb
djb

Reputation: 1

The way to refer to deleted row values is row original value from deleted rows table:

dt is datasource of datagridview
Dim dtDelete As DataTable = dt.GetChanges(DataRowState.Deleted)
cmd.Parameters.AddWithValue("@GridValue", r("Gridcolumn", DataRowVersion.Original))

the example is SQL but method is not specific

Upvotes: 0

Mysta
Mysta

Reputation: 79

Sorry my bad English. Step 1: Listen to click on a Datagrid

Private Sub datagridview_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles datagridview.CellClick

Step 2: Get what column click

datagridview.Rows.Item(e.RowIndex).Cells(0).Value

Step 3: Delete from Database

Dim sqlcmd As String = "delete  from [userinfo] where YourColumn = '" & datagridview.Rows.Item(e.RowIndex).Cells(0).Value & "'"

Fully code:

Private Sub Datagridview_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellClick
        Try
            Dim MyConn As OleDbConnection
            Dim connString As String = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Database3.Accdb;"
            MyConn = New OleDbConnection
            Dim cmdstr As String = "delete * from [userinfo] where YourColumn = '" & DataGridView.Rows.Item(e.RowIndex).Cells(0).Value & "'"
            Dim cmd As New OleDbCommand(cmdstr, MyConn)
            MyConn.Open()
            cmd.ExecuteNonQuery()
            MyConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 End Sub

Upvotes: 0

Ashish Emmanuel
Ashish Emmanuel

Reputation: 728

Delete * from [userinfo] will delete everything in the table. Pass your ID to be deleted. Eg. Delete * from [userinfo] where ID=5

You can use Datagridview.SelectedRows Property to find the selected Rows. Pass the ID Column index and you will get the ID.

Eg. "Delete * from [userinfo] where ID=" & DataGridView1.SelectedRows(0).Cells(0).Value.ToString()

If you Allow Multi-Select, Loop through the rows to delete. Also use parameters to pass variables to queries for security purposes.

Upvotes: 1

Related Questions