0

i need to be able to delete a row and its content via a datagrid viewer, the current code i have written only deletes the contents of row 0 and i cannot get it to do so at a selected row.

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim dataAdapter As New OleDbDataAdapter
    Dim DataTable As New DataTable
    Dim DataSet As New DataSet
    Try

        DataSet.Tables.Add(DataTable)
        Connection.Open()
        dataAdapter = New OleDbDataAdapter("SELECT * FROM Students", Connection)
        dataAdapter.Fill(DataTable)
        DataTable.Rows(0).BeginEdit()
        DataTable.Rows(0).Delete()
        DataTable.Rows(0).EndEdit()
        Dim Commandbuilder As New OleDbCommandBuilder(dataAdapter)
        dataAdapter.Update(DataTable)
        dgrStudentDatabaseViewer.DataSource = DataTable.DefaultView
        Connection.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Connection.Close()
    End Try
End Sub

Thanks in advance.

1
  • DataTable.Rows(0).Delete() you have it hardcoded to delete row(0) Commented Nov 11, 2015 at 19:57

1 Answer 1

1

With this code you can delete current selected row:

DataTable.Rows(DataGridView1.CurrentRow.Index).BeginEdit()
DataTable.Rows(DataGridView1.CurrentRow.Index).Delete()
DataTable.Rows(DataGridView1.CurrentRow.Index).EndEdit()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.