2 Replies - 8745 Views - Last Post: 08 December 2014 - 07:18 PM Rate Topic: -----

#1 Jommaria03   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-December 14

How to Delete Selected Record/Row in DataGridView and Ms Access

Posted 08 December 2014 - 05:58 PM

I have here a code and could someone fix it?

I have here the code of the delete button.
How to delete the selected rows in datagridview and also be deleted in Ms access. The code here only deletes the record one by one starting from the first row not when selecting a row.
I don't know what code to add. please help...



Private Sub DeleteData()
Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable


Try
If Microsoft.VisualBasic.Right(Application.StartupPat h, 1) = "\" Then
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "SampleDB.accdb;Persist Security Info=False;"
Else
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\SampleDB.accdb;Persist Security Info=False;"
End If
conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select id, image_description as description from tbl_image"
da.SelectCommand = cmd

da.Fill(dt)

Me.DataGridView1.DataSource = dt

dt.Rows(0).BeginEdit()
dt.Rows(0).Delete()
dt.Rows(0).EndEdit()

Dim cb As New OleDbCommandBuilder(da)

da.Update(dt)
Me.DataGridView1.DataSource = dt.DefaultView

Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: How to Delete Selected Record/Row in DataGridView and Ms Access

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: How to Delete Selected Record/Row in DataGridView and Ms Access

Posted 08 December 2014 - 07:03 PM

as your datagridview is bound to a datasource then by using
<yourDataGridView>.Rows.Remove(<yourDataGridView.CurrentRow)

this would remove the row that was currently selected.

After that, the update using the commandbuilder should apply whatever actions you performed to the database.

So instead of:
dt.Rows(0).BeginEdit()
dt.Rows(0).Delete()
dt.Rows(0).EndEdit()

Which only affects the first row of the DGV, you would remove the currentrow and then update the dataadapter passing it the datatable. You seem to already have this part, so replace the 3 lines above which are static to the first row with the remove code I supplied above.
Was This Post Helpful? 0
  • +
  • -

#3 Jommaria03   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-December 14

Re: How to Delete Selected Record/Row in DataGridView and Ms Access

Posted 08 December 2014 - 07:18 PM

View PostCharlieMay, on 08 December 2014 - 07:03 PM, said:

as your datagridview is bound to a datasource then by using
<yourDataGridView>.Rows.Remove(<yourDataGridView.CurrentRow)

this would remove the row that was currently selected.

After that, the update using the commandbuilder should apply whatever actions you performed to the database.

So instead of:
dt.Rows(0).BeginEdit()
dt.Rows(0).Delete()
dt.Rows(0).EndEdit()

Which only affects the first row of the DGV, you would remove the currentrow and then update the dataadapter passing it the datatable. You seem to already have this part, so replace the 3 lines above which are static to the first row with the remove code I supplied above.



So I replaced what you said. But this only deletes the selected row in DGV and the one I deleted is still in database. What else am I still missing?

      da.Fill(dt)
            DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
            Dim cb As New OleDbCommandBuilder(da)
            da.Update(dt)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1